Ascending sequence (Greedy + clever) programming questions, sequential greedy

Source: Internet
Author: User

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

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.