Test instructions
① first defines S as an ordered sequence, s={A1, A2, A3, ..., an},n is the number of elements;
② then defines a sub sequence to be taken out of S, sub={Ai1, Ai2, Ai3, ..., Aim},m is the number of elements;
③ where sub satisfies Ai1 < Ai2 < Ai3 < ... < Aij-1 < Aij < Aij+1 < ... < Aim;
④ at the same time sub satisfies for any connected two Aij-1 and AIJ have ij-ij-1 > D (1 < J <= M, D for a given integer);
⑤ obviously satisfies such sub-subsequence there are many, and in these sub-sequence sub-sequences, the number of elements is called "xiaoming sequence" (that is, m the largest sub-subsequence).
For example: Sequence s={2,1,3,4}, where d=1;
Can get "xiaoming sequence" of the m=2. That is, sub={2,3} or {2,4} or {1,4} are all "xiaoming sequences".
When Xiao Ming invented the "xiaoming sequence" that moment, the emotion is very excited, so that the mind is messy, so he wants to ask you to help him to calculate in the given S-sequence and the whole number D, how many of the elements in the "xiaoming sequence" need?
Ideas:
DP idea, but can only think of n^2 algorithm. Hey, there is a topic to say (0<=AI<=10^5), that is, with the line tree to save the most value.
Every time you do a problem, you have to think about it, the border,
D=0 alone with the greedy method of calculation, in fact, do not have to,.
Code:
int ConstN =100005;intA[n], f[n];intf[n<<2];intn,d;voidPushup (intRT) {F[rt]=max (f[rt<<1],f[rt<<1|1] ); return;}voidBuildintLintRintRT) { if(l==R) {F[rt]=0; return; } intM= (l+r) >>1; Build (Lson); Build (Rson); Pushup (RT);}voidUpdateintPosintXintLintRintRT) { if(l==S) {F[rt]=Max (f[rt],x); return; } intM= (l+r) >>1; if(pos<=m) update (Pos,x,lson); ElseUpdate (Pos,x,rson); Pushup (RT);}intQueryintLintRintLintRintRT) { if(L<=l && r<=s) { returnF[rt]; } intM= (l+r) >>1; intres=0; if(l<=m) Res=Max (Res,query (L,r,lson)); if(m<R) Res=Max (Res,query (L,r,rson)); returnRes;}intProc1 () {intD[n]; intcn=0; d[0]=-1; Rep (I,1, N) { if(a[i]>D[CN]) {d[++cn]=A[i]; }Else{ intPos=lower_bound (d+1, d+1+cn,a[i])-D; D[pos]=A[i]; } } returncn;}intMain () { while(SCANF ("%d%d", &n,&d)! =EOF) { intes=-inf; Rep (I,1, N) {scanf ("%d",&A[i]); Es=Max (es,a[i]); } if(d==0){ intans=Proc1 (); printf ("%d\n", ans); }Else{Build (0Es1); intans=1; Rep (I,1, N) { if(i-d-1<=0) {F[i]=1; }Else{update (A[i-D1],f[i-d-1],0Es1);//Pos,x,l,r,rt if(a[i]==0) {F[i]=1; Continue; } intT=query (0, a[i]-1,0Es1);//L,r,l,r,rtf[i]=t+1; Ans=Max (ans,f[i]); }} printf ("%d\n", ans); } } return 0;}
Hdu 4521 Xiao Ming sequence (line tree, DP idea)