UVA 1471 Defense Lines line of defense

Source: Internet
Author: User

Given a sequence of length n, it is required to delete a continuous subsequence so that the remainder of the sequence has a continuous increment subsequence of the maximum length.

The simplest idea is to enumerate the starting J and the end point I, and then count the longest lengths that can be extended forward or backward, in the notation G (i) and f (i). The lengths at which each point can be stretched forward and backward can be preprocessed first (g (i) and f (i)). Then enumerate the end J, quickly find a G (j) the largest starting point. If there are two candidate starting points, a J, a J ', A[j ']<=a[j] and G[j ']>g[j], then J must be excluded, g (j ') larger, and easier to splice.

In the case of fixed I, all of the valuable (A[j],g (j)) sorted by A[j] (A[j] The same value retains the G (j) of the large one), will form an ordered table, according to the previous conclusion G (j) is incremental, orderly, then the two-point lookup is useful.

Then consider that the effect of the change I on the previous ordered table, I increase, the previous a[i],g (i) added to the ordered table, if satisfies A[i '] than it a[i] small and g (i ') the largest two-tuple, that is, one of its front elements, which satisfies g (i ') >=g (i), Then this element should not be preserved. Otherwise it should be added to this two-tuple, after adding this two-tuple, in order to maintain the nature of the ordered table, but also to check the deletion of some g (i*) small elements.

Finally think more thoroughly, the implementation of the way is set, with the pair to ensure the two-tuple, pair comparison is the first dimension, compared to the second dimension. As for the second implementation, use the array to save G (j) corresponding to the smallest a[j] value, review the LIS and then fill it ~

#include <bits/stdc++.h>#defineMP Make_pair#defineSe Second#defineFi firstusing namespacestd;Const intMAXN = 2e5+5; typedef pair<int,int>PII;intA[MAXN];intG[MAXN];//<-intF[MAXN];// -Set<pii>s;intMain () {intT scanf"%d",&T);  while(t--){        intN scanf"%d",&N);  for(inti =0; I < n; i++) scanf ("%d", A +i); if(n = =1) {printf ("1\n");Continue; } g[0] =1;  for(inti =1; I < n; i++) {            if(a[i]>a[i-1]) G[i] = g[i-1]+1; ElseG[i] =1; } f[n-1] =1;  for(inti = n2; I >=0; i--) {            if(a[i]<a[i+1]) F[i] = f[i+1]+1; ElseF[i] =1; } s.clear (); S.insert (MP (a[0],g[0])); intAns =1;  for(inti =1; I < n; i++) {PII cur (a[i],g[i]); Set<pii>::iterator it =s.lower_bound (cur); BOOLKeep =true; if(it!=S.begin ()) {It--; Ans= Max (ans,it->se+F[i]); Keep= It->se <cur.se; }            if(Keep) {s.erase (cur); S.insert (cur); It++; it++;  while(It! = S.end () && it->se <= cur.se) s.erase (it++); }} printf ("%d\n", ans); }    return 0;}

UVA 1471 Defense Lines line of defense

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.