Ultraviolet A 481-What Goes Up

Source: Internet
Author: User

Question: Maximum ascending subsequence.

Analysis: dp, lis, binary, monotonous queue. Lis o (nlogn) algorithm. This algorithm uses monotonous queue + binary optimization. The element Q [I] In the monotonic queue is the smallest ending element in LIS whose length is I so far. During running, if the current element is smaller than the team-end element, the LIS can be formed in front of the team, directly adding the team-end element. Otherwise, the end element of the LIS with the same length as the previous one will be replaced, the elements at the corresponding position are reduced. Because the queue satisfies the monotonicity, binary search is used to improve the efficiency. For example, if the sequence is {,}, then the calculation process Q is {1}, {, 3 }. This is because {1, 3} can constitute the LIS prefix string, then {1, 2} can constitute the LIS prefix, and can accept more suffixes. Www.2cto.com

Note: The O (n ^ 2) algorithm with a large data size will use TLE and find that the final LIS can be directly satisfied using the monotonous queue.

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
 
Int data [100000];
Int length [100000];
Int front [100000];
Int Q [100000];
 
Void output (int v)
{
If (front [v])
Output (front [v]);
Printf ("% d \ n", data [v]);
}
 
Int bs (int r, int v)
{
Int l = 1;
While (l <r ){
Int mid = (l + r)> 1;
If (data [Q [mid] <v)
L = mid + 1;
Else r = mid;
}
Return r;
}
 
Int main ()
{
Int count = 1;
While (scanf ("% d", & data [count])! = EOF)
Count ++;

Int tail = 0;
Q [++ tail] = 1;
For (int I = 2; I <count; ++ I)
If (data [Q [tail] <data [I]) {
Q [++ tail] = I;
Front [I] = Q [tail-1];
} Else {
Int id = bs (tail, data [I]);
Q [id] = I;
Front [I] = Q [id-1];
}

Printf ("% d \ n-\ n", tail );
Output (Q [tail]);
Return 0;
}

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.