Longest Ordered Subsequence
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 1884, Accepted users: 1572
Problem 10001 : No special judgement
Problem 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 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 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
Problem Source
HNU Contest

 

 1 #include<stdio.h>  
 2 
 3 int main()  
 4 { 
 5     int a[1001],b[1001],n,i,j,s=0;  
 6     scanf("%d",&n);  
 7     for(i=0;i<n;i++)  
 8     { 
 9         scanf("%d",&a[i]);  
10         b[i]=1;  
11     }  
12     for(i=0;i<n-1;i++) 
13         for(j=i+1;j<n;j++)  
14             if(a[j]>a[i] && b[j]<=b[i])  
15                 b[j]=b[i]+1;  
16             for(i=0;i<n;i++)  
17                 if(b[i]>s)  
18                     s=b[i];  
19     printf("%d\n",s);  
20   return 0; 
21 }  
posted on 2012-07-19 21:46  黑色的铅笔  阅读(752)  评论(0编辑  收藏  举报