zoukankan      html  css  js  c++  java
  • Longest Ordered Subsequence

    A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1a2, ..., aN) be any sequence ( ai1ai2, ..., 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

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    #define N 10005
    int a[N], dp[N];
    
    int main()
    {
        int n, m, i, j, sum;
        while(scanf("%d", &n)!=EOF)
        {
            memset(a, 0, sizeof(a));
            memset(dp, 0, sizeof(dp));
            for(i=0;i<n;i++)
            {
                scanf("%d", &a[i]);
            }
            sum=0;
            for(i=1;i<n;i++)
            {
                for(j=0;j<i;j++)
                {
                    if(a[i]>a[j])
                    {
                        dp[i]=max(dp[j]+1, dp[i]);
                    }
                }
               sum=max(sum, dp[i]);
            }
            printf("%d\n", sum+1);
        }
        return 0;
    }

    还有一个错误的,因为过了不少样例,卡着我一直找不出来

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    #define N 10005
    int a[N], dp[N];
    
    int main()
    {
        int n, m, i, j, sum;
        while(scanf("%d", &n)!=EOF)
        {
            memset(a, 0, sizeof(a));
            memset(dp, 0, sizeof(dp));
            for(i=0;i<n;i++)
            {
                scanf("%d", &a[i]);
            }
            dp[0]=1;
            sum=0;
            for(i=1;i<n;i++)
            {
                for(j=0;j<i;j++)
                {
                    if(a[i]>a[j])
                    {
                        dp[i]=max(dp[j], dp[i]);
                    }
                }
                dp[i]++;
                sum=max(sum, dp[i]);
            }
            printf("%d\n", sum);
        }
        return 0;
    }
  • 相关阅读:
    程序员通过什么渠道接外包项目
    中小型软件项目开发一般流程建议
    DevExpress GridControl功能总结
    页面UI注意事项,你在乎吗?
    加密,解密
    本地化(国际化)
    AutoLayout(自动布局)
    UItableView与UICollectionView
    分享iOS开发常用(三方类库,工具,高仿APP,实用网站,技术干货)
    NSPredicate
  • 原文地址:https://www.cnblogs.com/zct994861943/p/6800123.html
Copyright © 2011-2022 走看看