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;
}