Test instructions
Sequence of n numbers, finding the longest ascending subsequence of each digit interval greater than D
Analysis:
The most basic DP but the large data volume O (n^2) must timeout before dp[i] for the longest ascending subsequence is by the first dp[1]---dp[i-d-1] meet the maximum value of the condition, we can use the segment tree to maintain DP[1]---dp[i-d-1] maximum
#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<1|1#defineAll 1,n,1#defineRead Freopen ("In.txt", "R", stdin)#defineN 100010Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;intmaxv[n*4],a[n],dp[n];voidPushup (intRT) {Maxv[rt]=max (maxv[rt<<1],maxv[rt<<1|1]);}voidBuildintLintRintRT) {Maxv[rt]=0; if(l==R) { return; } intM= (l+r) >>1; Build (Lson); Build (Rson);}voidUpdateintPosintNumintLintRintRT) { if(l==R) {Maxv[rt]=Max (maxv[rt],num); return; } intM= (l+r) >>1; if(pos<=m) update (Pos,num,lson); ElseUpdate (Pos,num,rson); Pushup (RT);}intQueryintLintRintLintRintRT) { if(l<=l&&r>=R) { returnMaxv[rt]; } intM= (l+r) >>1; intans=0; if(l<=m) ans=Max (Ans,query (L,r,lson)); if(r>m) ans=Max (Ans,query (L,r,rson)); returnans;}intMain () {intn,d; while(~SCANF ("%d%d",&n,&d)) { intMaxl=0; for(intI=1; i<=n;++i) {scanf ("%d",&A[i]); Maxl=Max (maxl,a[i]); } memset (DP,0,sizeof(DP)); Build (0, Maxl,1);//make a range of values intmaxans=0; for(intI=1; i<=n;++i) { if(i-d-1>0) {Update (A[i-D1],dp[i-d-1],0, Maxl,1);//Update Point } if(a[i]==0) {Dp[i]=1; } ElseDp[i]=query (0, a[i]-1,0, Maxl,1)+1;//value <a[i] and dp[1]---dp[i-d-1] max .maxans=Max (maxans,dp[i]); } printf ("%d\n", Maxans); }return 0;}
HDU 4521-xiaoming sequence (line tree good question)