HDU 4521 Xiao Ming series problems-Xiao Ming sequence (line segment tree maintenance DP)
Address: HDU 4521
The basic idea is DP. When finding the maximum value of the preceding number, you can use the line segment tree to save time.
Because the interval must be greater than d. Therefore, you can use a queue to delay the update to ensure that all requests are sent before d.
The Code is as follows:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include using namespace std;#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1#define LL __int64const int INF=0x3f3f3f3f;struct node{ int maxv, num;};int maxv[400000], a[1100000];int q_maxv;void PushUp(int rt){ maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);}void Update(int p, int x, int l, int r, int rt){ if(l==r) { maxv[rt]=p; return ; } int mid=l+r>>1; if(x<=mid) Update(p,x,lson); else Update(p,x,rson); PushUp(rt);}void Query(int ll, int rr, int l, int r, int rt){ if(ll<=l&&rr>=r) { q_maxv=max(q_maxv,maxv[rt]); return ; } int mid=l+r>>1; if(ll<=mid) Query(ll, rr, lson); if(rr>mid) Query(ll,rr,rson);}int main(){ int n, d, i, j, top; node tmp, now; while(scanf("%d%d",&n,&d)!=EOF) { queue
q; for(i=0;i
=d) { tmp=q.front(); q.pop(); Update(tmp.maxv+1,tmp.num,0,100000,1); } } while(!q.empty()) { tmp=q.front(); q.pop(); Update(tmp.maxv+1,tmp.num,0,100000,1); } printf("%d\n",maxv[1]); } return 0;}