poj2533 Longest Ordered Subsequence

Longest Ordered Subsequence
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 54860   Accepted: 24584

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

 

LIS

 1 #include <cstdio>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 
 6 int n, a[1010], f[1010], ans;
 7 
 8 int main() {
 9   scanf("%d", &n);
10   for(int i = 1 ; i <= n ; i ++) {
11     scanf("%d", &a[i]);
12     f[i] = 1;
13     for(int j = 1 ; j < i ; j ++) {
14       if(a[j] < a[i]) {
15         f[i] = max(f[i], f[j] + 1);
16       }
17     }
18     ans = max(ans, f[i]);
19   }
20   printf("%d\n", ans);
21 }
View Code

 

求个数(c数组)?

c[i] = 1

if(f[j] + 1 == f[i] && a[j] < a[i]) c[i] += c[j];

posted @ 2017-08-19 10:31  KingSann  阅读(79)  评论(0编辑  收藏  举报