1576 maximum strict ascending subsequence, 1576 longest rising
1576 maximum strict ascending subsequence
Time Limit: 1 s space limit: 256000 KB title level: Gold QuestionDescription
Description
For an array a1, a2... an, find the longest ascending/descending subsequence ab1 <ab2 <... <abk, where b1 <b2 <... bk.
The output length.
Input description
Input Description
The first line is an integer N.
The second row, N integers (N <= 5000)
Output description
Output Description
Output the maximum value of K, that is, the maximum length of the sub-sequence is not decreased.
Sample Input
Sample Input
5
9 3 6 2 7
Sample output
Sample Output
3
Data range and prompt
Data Size & Hint
[Example]
The longest non-descent subsequence is 3, 6, and 7.
Bare question!
1 # include <iostream> 2 # include <cstdio> 3 using namespace std; 4 const int MAXN = 10001; 5 int a [MAXN]; 6 int f [MAXN]; // length 7 int main () 8 {9 int n; 10 scanf ("% d", & n); 11 for (int I = 1; I <= n; I ++) 12 scanf ("% d", & a [I]); 13 for (int I = 1; I <= n; I ++) 14 f [I] = 1; 15 for (int I = 1; I <= n; I ++) 16 {17 for (int j = 1; j <I; j ++) 18 {19 if (a [I]> = a [j]) 20 {21 // f [I] = max (f [j] + 1, f [I]); 22 f [I] = f [j] + 1; 23} 24} 25} 26 printf ("% d", f [n]); 27 return 0; 28}