POJ - 2533 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

 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 }

 

posted @ 2017-07-26 09:34  wydxry  阅读(182)  评论(0编辑  收藏  举报
Live2D