3955 maximum strict ascending subsequence (enhanced version), 3955 enhanced version
Time Limit: 1 s space limit: 256000 KB title level: DiamondQuestionView running resultsDescriptionDescription
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 <= 1000000)
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
N <= 1000000
To facilitate debugging, the data name has been modified -- THREE
CATEGORY tag
Tags click here to expand
Nlogn solution, using the monotonicity of functions
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 using namespace std; 7 const int maxn = 0x7ffff; 8 int n; 9 int dp [10000001]; // The length of the longest ascending subsequence of I is 10 int a [10000001]; 11 int main () 12 {13 scanf ("% d", & n); 14 for (int I = 1; I <= n * 2; I ++) 15 dp [I] = maxn; 16 for (int I = 1; I <= n; I ++) 17 scanf ("% d", & a [I]); // dp [1] = min (dp [1], a [I]); 18 for (int I = 1; I <= n; I ++) 19 {20 int p = upper _ Bound (dp + 1, dp + n, a [I])-dp; 21 if (a [I]! = Dp [P-1]) 22 dp [p] = a [I]; 23} 24 for (int I = 1; I <= n + 1; I ++) 25 if (dp [I] = maxn) 26 {27 printf ("% d", I-1); 28 break; 29} 30 return 0; 31}