Test Instructions:Link
Method:Treap+lis
parsing:Just saw the question also blindfolded for a while, do not know AH--! later thinking to find the law. Drawing examples and the two sets of data have been made up to find point problems ... The answer increments? And then the reaction comes--when we get into the big insert, the post-insertion situation does not affect the pre-insertion. So we can get the whole sequence out first? After a LIS, for each number, he has an LIS value, of course, this may not be the best solution after he inserted, because the previous value may be greater than after he inserted. So we need to sweep the array again and update the answer. As for the first part to find the sequence, it is very advantageous to realize with Treap. What about the second part? N^2 's practice is obviously not here, so Nlogn is good. Large complexity O (NLOGN)
Code:
#include <ctime>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#define N 100010using namespace Std;intSize,n,root;struct node{intL,r,rnd,siz,w,v;}TR[N];void Pushup (intRT) {TR[rt].siz=TR[TR[rt].l].siz+TR[TR[rt].r].siz+1; }void Lturn (int&RT) {intt=TR[RT].R;TR[rt].r=TR[T].l;TR[T].l=rt;TR[t].siz=TR[Rt].siz; Pushup (RT); rt=t;} void Rturn (int&RT) {intt=TR[Rt].l;TR[rt].l=TR[T].R;TR[T].r=rt;TR[t].siz=TR[Rt].siz; Pushup (RT); rt=t;} void Insert (int&rt,intV) {if(!RT) {rt=++size;TR[rt].l=TR[rt].r=0,TR[rt].siz=1;TR[Rt].v=size,TR[rt].rnd=Rand();return; }TR[rt].siz++;if(v<=TR[TR[rt].l].siz+1) {Insert (TR[Rt].l,v];if(TR[TR[rt].l].rnd<TR[Rt].rnd) Rturn (RT); }Else{Insert (TR[rt].r,v-TR[TR[rt].l].siz-1);if(TR[TR[rt].r].rnd<TR[Rt].rnd) Lturn (RT); }}intTotintSeQ[n]; void Getseq (int rt){if(TR[rt].l!=0) GetseQ (TR[RT].L); SeQ[++tot]=TR[RT].V;if(TR[rt].r!=0) GetseQ (TR[RT].R);}intF[n],d[n],ans[n];int Print[N]; void Getlis () {memset (d,0x3f, sizeof (d)); d[0]=0; d[1]=seQ[1], f[1]=1;intmx=1; for(intI=2; i<=n;i++) {if(SEQ[i]>D[MX]) {f[i]=++mx; D[mx]=min (D[mx],seQ[i]); }Else{intL=0, r=mx,ans=0; while(L<=R) {intMid= (l+r) >>1;if(D[mid]<seQ[i]) ans=mid,l=mid+1;Elser=mid-1; } f[i]=ans+1; d[ans+1]=min (d[ans+1],seQ[i]); } }}intMain () {scanf ("%d", &n); for(intI=1; i<=n;i++) {int x; scanf"%d",&x);x++; Insert (Root,x); } GetseQ (Root); Getlis (); for(intI=1; i<=n;i++)Print[SEQ[i]]=f[i]; for(intI=1; i<=n;i++)Print[I]=max (Print[I],Print[I1]); for(intI=1; i<=n;i++)printf("%d\ n",Print[i]);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 3173 [Tjoi2013] Longest ascending sub-sequence Treap+lis