3955 maximum strict ascending subsequence (enhanced version), 3955 enhanced version

Source: Internet
Author: User

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}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.