• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj2533

            Longest Ordered Subsequence

            Time Limit: 2000MS Memory Limit: 65536K
            Total Submissions: 21310 Accepted: 9190

            Description

            A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

            Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

            Input

            The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

            Output

            Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

            Sample Input

            7
            1 7 3 5 9 4 8

            Sample Output

            4
            
            最長上升子序列
            樸素的做法n^2的復雜度,n<=1000不會超時
            有nlogn的做法
             1//////樸素做法
             2#include<stdio.h>
             3#include<string.h>
             4#include<math.h>
             5#define MAX 1005
             6int a[MAX],f[MAX];
             7int n,i,j,ans;
             8int max(int a,int b)
             9{
            10    if (a>b) return a; else return b;
            11}

            12int main()
            13{
            14    scanf("%d",&n);
            15    for (i=1;i<=n ;i++) scanf("%d",&a[i]);
            16    f[1]=1;
            17    ans=f[1];
            18    for (i=2;i<=n ;i++ )
            19    {
            20        f[i]=1;
            21        for (j=1;j<=i-1 ;j++ )
            22        {
            23            if (a[j]<a[i])
            24            {
            25                f[i]=max(f[i],f[j]+1);
            26            }

            27        }

            28        ans=max(ans,f[i]);
            29    }

            30    printf("%d\n",ans);
            31    return 0;
            32}

            優(yōu)化的解法 nlogn
             1////二分查找優(yōu)化 nlogn
             2#include<stdio.h>
             3#include<string.h>
             4#include<math.h>
             5#define MAX 1005
             6int f[MAX];
             7int i,j,ans,n;
             8int left,right,x,mid;
             9int main()
            10{
            11    scanf("%d",&n);
            12    ans=0;
            13    for (i=1; i<=n ; i++)
            14    {
            15        scanf("%d",&x);
            16        left=1;
            17        right=ans;
            18        while (left<right)
            19        {
            20            mid=(left+right)/2;
            21            if (f[mid]<x) left=mid+1;
            22            else right=mid;
            23        }

            24        if (left>=right&&x>f[ans]||ans==0)
            25        {
            26            ans++;
            27            f[ans]=x;
            28        }

            29        else if (x<f[left])
            30        {
            31            f[left]=x;
            32        }

            33    }

            34    printf("%d\n",ans);
            35    return 0;
            36}

            posted on 2012-02-21 14:00 jh818012 閱讀(235) 評論(0)  編輯 收藏 引用

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統(tǒng)計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內(nèi)容較長,點擊標題查看
            • --王私江
            久久婷婷五月综合色奶水99啪| 婷婷五月深深久久精品| 久久香蕉国产线看观看99| 久久水蜜桃亚洲av无码精品麻豆| 久久精品国产亚洲AV久| 国产精品久久久久久福利漫画| 久久人人爽人爽人人爽av| 亚洲一级Av无码毛片久久精品| 无码AV中文字幕久久专区| 久久精品一区二区三区中文字幕| 国内精品久久国产| 免费观看成人久久网免费观看| 久久久这里有精品| 久久国产乱子精品免费女| 超级碰碰碰碰97久久久久| 一本伊大人香蕉久久网手机| 亚洲狠狠婷婷综合久久久久| 久久国产一片免费观看| 久久久国产精品网站| 亚洲αv久久久噜噜噜噜噜| 天天影视色香欲综合久久| 99热成人精品热久久669| 久久久久久精品免费看SSS| 久久久久国产一级毛片高清板| 久久久国产精品福利免费| 国产精品久久久久久吹潮| 久久国语露脸国产精品电影| 香蕉久久永久视频| 久久一区二区免费播放| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 久久这里只有精品视频99| 国产成人久久精品激情| 久久亚洲春色中文字幕久久久| 女人高潮久久久叫人喷水| 久久久久免费视频| 久久夜色撩人精品国产| 亚洲v国产v天堂a无码久久| 伊人久久无码精品中文字幕| 亚洲日本久久久午夜精品| 欧美精品国产综合久久| 欧美一区二区久久精品|