POJ 2533 Longest Ordered Subsequence

题目内容
Longest Ordered Subsequence
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20761 Accepted: 8973

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

Source
Northeastern Europe 2002, Far-Eastern Subregion

简单动规:最长上升子序列

Accepted 392K 0MS G++ 775B  

 

编译环境:Linux ubuntu 2.6.35-30-generic  g++ 4.4.5 VIM

View Code
 1 #include<cstdio>
2 #include<cstring>
3 const int MAXN=1000;
4 int num[MAXN],B[MAXN],len,N;
5 int LIS(int *array,int n);
6 int BiSearch(int *b,int len,int w);
7 int main(void)
8 {
9 scanf("%d",&N);
10 for(long i=0;i!=N;i++)
11 scanf("%d",&num[i]);
12 printf("%d\n",LIS(num,N));
13 return 0;
14 }
15 int LIS(int *array,int n)
16 {
17 int len=1;
18 B[0]=array[0];
19 for(int i=1;i!=n;i++)
20 if(array[i]>B[len-1])
21 {
22 B[len++]=array[i];
23 }
24 else
25 B[BiSearch(B,len,array[i])]=array[i];
26 return len;
27 }
28 int BiSearch(int *b,int len,int w)
29 {
30 int left=0,right=len-1,middle;
31 while(left<=right)
32 {
33 middle=(left+right)/2;
34 if(b[middle]>w)
35 right=middle-1;
36 else
37 if(b[middle]<w)
38 left=middle+1;
39 else
40 return middle;
41 }
42 return b[middle]>w?middle:middle+1;
43 }



posted on 2011-11-04 20:38  SilVeRyELF  阅读(200)  评论(0编辑  收藏  举报

导航