Ascending sequence (Greedy + clever) programming questions, sequential greedy
Question
Calculate the monotonic ascending subsequence of the specified length, and output the smallest lexicographic sequence.
Think about the longest ascending subsequence: f [I]: indicates the length of the longest ascending subsequence (from left to right) in the sequence ending with I ), let's change our mindset-from right to left, f [I] indicates the length of the longest ascending subsequence in the suffix with I as the starting point, in this way, we can use the NlogN solution to process the data before greedy and store the data in the struct. At the same time, the length of the longest ascending subsequence of the entire sequence is processed.
Additionally, the struct stores the values v and id.
The following is greedy;
If
The length of the ascending sub-sequence after this point is greater than the number of products to be selected (that is, x-now, now indicates the current length)
The id of the next vertex (that is, the position in the original sequence) is greater than the id of the previous vertex.
In the original question, the price is raised monotonically, so there must be no repetition.
When the preceding three points are met, this point can be output;
Because we have sorted v as the first keyword and id as the second keyword, each selection is the solution with the smallest Lexicographic Order. The greedy correctness is proved.
Code
Code
# Include
# Include
# Include
Using namespace std; const int maxn = 100001; int n, m, x, f [maxn], q, ans [maxn], tot; struct st {int v, id, len; // v weight, id number, length of the longest ascending subsequence starting with len: I} a [maxn]; int read () {int now = 0, f = 1; char ch = getchar (); while (ch <'1' | ch> '9') {if (ch = '-') f =-1; ch = getchar () ;}while (ch >='0' & ch <= '9') {now = (now <1) + (now <3) + ch-'0'; ch = getchar ();} return now * f;} bool cmp (st x, st y) {if (x. v = y. v) return x. id
= 1; I --) {for (int j = I + 1; j <= n; j ++) if (a [I]. vmaxx) {printf ("LXALWAYSMISS \ n"); continue;} for (int I = 1; I <= n; I ++) {if (now = x) break; if (a [I]. len> = x-now & a [I]. id> a [last]. id & a [I]. v> a [last]. v) {now ++; last = I; printf ("% d", a [I]. v) ;}} printf ("\ n") ;}return 0 ;}