zoukankan      html  css  js  c++  java
  • POJ

    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

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 int n,a[20010],c[20010],len=0;
     7 int Find(int x)
     8 {
     9     int l=1,r=len,mid;
    10     while(l<=r){
    11         mid=(l+r)>>1;
    12         if(x>c[mid]){ //记忆方法:求上升序列,就表示x更大,那么就是大于
    13             l=mid+1;
    14         }else r=mid-1;
    15     }
    16     return l;
    17 }
    18 int main()
    19 {
    20     scanf("%d",&n);
    21     for(int i=1;i<=n;i++)
    22         scanf("%d",&a[i]);
    23     for(int i=1;i<=n;i++){
    24         int k=Find(a[i]);
    25         c[k]=a[i];
    26         len=max(len,k);
    27     }
    28     printf("%d",len);
    29     return 0;
    30 }
  • 相关阅读:
    VMware Tools的安装
    XmlSerializer
    string[][]和string[,] 以及 int[][]和int[,]
    Get Length 使用dynamic关键字
    Cocos2d-x 3.0final 终结者系列教程01-无论是从cocos2d-x2.x升级到版本cocos2d-x3.x
    Hosting WCF Service
    A*算法进入
    OpenCV面、人眼检测
    JAVA学习篇--JAVA两种编程模式控制
    采用Java语言如何实现高速文件复制?
  • 原文地址:https://www.cnblogs.com/wydxry/p/7237876.html
Copyright © 2011-2022 走看看