zoukankan      html  css  js  c++  java
  • POJ2533——DP(LCS+LDS)——Longest Ordered Subsequence

    Description

    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
    

    Source

    Northeastern Europe 2002, Far-Eastern Subregion
    大意:没什么好说的~模板题,取最大就行
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    const int MAX = 11000;
    int a[MAX],b[MAX];
    int main()
    {
        int n;
        int l,r,mid;
        scanf("%d",&n);
        for(int i = 1; i <= n ;i++)
            scanf("%d",&a[i]);
            // LCS
        b[1] = a[1];
        int k,i;
        for(i = 2, k = 1; i <= n ;i++){
                if(a[i] > b[k]) b[++k] = a[i];
               else {
                l = 1; r = k;
                  while(l<=r){
                       mid = (l + r) >> 1;
                       if(b[mid] < a[i]) l = mid + 1;
                       else if(b[mid] > a[i]) r = mid - 1;
                       else break;
                  }
                  b[l] =a[i];
               }
        }
        int temp1 = k;
        // LDS
        memset(b,0,sizeof(b));
        b[1] = a[1];
        for(i = 2, k = 1; i <= n ;i++){
               if(a[i] < b[i]) b[++k] = a[i];
              else {
                  l = 1; r = k;
                 while(l <= r){
                  mid = (l + r) >> 1;
                 if(b[mid] > a[i]) l = mid + 1;
                 else if(b[mid] < a[i]) r = mid -1;
                 else break;
                 }
                 b[l] = a[i];
              }
        }
        int temp2 = k;
        printf("%d",max(temp1,temp2));
        return 0;
    }
    View Code
  • 相关阅读:
    go语言-值类型与引用类型
    MongoDB小东西
    2018年12月29日
    父进程结束,其子进程不会结束,会挂到init进程下
    python处理Windows平台上路径有空格
    python print 在windows上 出现 Bad file descriptor error
    docker私有镜像仓库搭建
    docker 在centos6 和centos7上的区别
    virtualbox 设置centos7 双网卡上网
    Centos6.6安装docker
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4366079.html
Copyright © 2011-2022 走看看