Special Subsequencetime limit:5000msmemory limit:32768kbthis problem would be judged onZju. Original id:3349
64-bit integer IO format: %lld Java class name: Main
There a sequence S with n integers, and a are a special subsequence that satisfies | ai-ai-1| <= D (0 <i<=| a|))
Now your task was to find the longest special subsequence of a certain sequence S
Input
There is no more than cases, process till the End-of-file
The first line of all case contains, n and D (1<=n<=100000, 0<=d<=100000000) as in the description.
The second line contains exact n integers, which consist the sequnece S . Each integer was in the range [0,100000000]. There is the blank between each integer.
There is a blank line between the cases
Output
For each case, print the maximum length of special subsequence you can get.
Sample Input
5 21 4 3 6 55 01 2 3 4 5
Sample Output
31
SourceZOJ Monthly, June 2010AuthorCHEN, Zhangyi Problem solving: Dynamic programming + segment Tree optimization
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =100100;4 inttree[maxn<<2],lisan[maxn],a[maxn],tot,n,d;5 intQueryintLintRintLtintRtintv) {6 if(LT <= L && RT >= R)returnTree[v];7 intMid = (L + R) >>1, ret =0;8 if(LT <= mid) ret = query (l,mid,lt,rt,v<<1);9 if(Rt > Mid) ret = max (Ret,query (Mid +1,r,lt,rt,v<<1|1));Ten returnret; One } A voidUpdateintLintRintPosintValintv) { - if(L = =R) { -TREE[V] =Max (tree[v],val); the return; - } - intMid = (L + R) >>1; - if(POS <= mid) Update (l,mid,pos,val,v<<1); + if(Pos > Mid) Update (Mid +1,r,pos,val,v<<1|1); -TREE[V] = max (tree[v<<1],tree[v<<1|1]); + } A intMain () { at while(~SCANF ("%d%d",&n,&d)) { -memset (Tree,0,sizeoftree); - for(inti =0; I < n; ++i) { -scanf"%d", A +i); -Lisan[i] =A[i]; - } inSort (Lisan,lisan +n); -tot = unique (Lisan,lisan + N)-Lisan; to intRET =0; + for(inti =0; I < n; ++i) { - intL = Max (0,(int) (Lower_bound (Lisan,lisan + tot,a[i]-D)-Lisan +1)); the intR = min (Tot, (int) (Upper_bound (Lisan,lisan + tot,a[i] + D)-Lisan)); * intTMP =1, pos = lower_bound (Lisan,lisan + tot,a[i])-Lisan +1; $ if(L <= R) tmp = query (0, Tot,l,r,1) +1;Panax NotoginsengRET =Max (ret,tmp); -Update0, Tot,pos,tmp,1); the } +printf"%d\n", ret); A } the return 0; +}
View Code
ZOJ 3349 Special subsequence