您的位置:首页 > 产品设计 > UI/UE

ZOJ 3349 Special Subsequence

2015-10-03 11:09 561 查看

Special Subsequence

Time Limit: 5000ms
Memory Limit: 32768KB
This problem will be judged on ZJU. Original ID: 3349
64-bit integer IO format: %lld Java class name: Main

There a sequence S with n integers , and A is a special subsequence that satisfies |Ai-Ai-1| <= d ( 0 <i<=|A|))

Now your task is to find the longest special subsequence of a certain sequence S

Input

There are no more than 15 cases , process till the end-of-file

The first line of each case contains two integer 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 is in the range [0,100000000] .There is blank between each integer.

There is a blank line between two cases

Output

For each case , print the maximum length of special subsequence you can get.

Sample Input

5 2
1 4 3 6 5

5 0
1 2 3 4 5

Sample Output

3
1


Source

ZOJ Monthly, June 2010

Author

CHEN, Zhangyi

解题:动态规划+线段树优化

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int tree[maxn<<2],Lisan[maxn],a[maxn],tot,n,d;
int query(int L,int R,int lt,int rt,int v){
if(lt <= L && rt >= R) return tree[v];
int mid = (L + R)>>1,ret = 0;
if(lt <= mid) ret = query(L,mid,lt,rt,v<<1);
if(rt > mid) ret = max(ret,query(mid + 1,R,lt,rt,v<<1|1));
return ret;
}
void update(int L,int R,int pos,int val,int v){
if(L == R){
tree[v] = max(tree[v],val);
return;
}
int mid = (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]);
}
int main(){
while(~scanf("%d%d",&n,&d)){
memset(tree,0,sizeof tree);
for(int i = 0; i < n; ++i){
scanf("%d",a + i);
Lisan[i] = a[i];
}
sort(Lisan,Lisan + n);
tot = unique(Lisan,Lisan + n) - Lisan;
int ret = 0;
for(int i = 0; i < n; ++i){
int L = max(0,(int)(lower_bound(Lisan,Lisan + tot,a[i] - d) - Lisan + 1));
int R = min(tot,(int)(upper_bound(Lisan,Lisan + tot,a[i] + d) - Lisan));
int tmp = 1,pos = lower_bound(Lisan,Lisan + tot,a[i]) - Lisan + 1;
if(L <= R) tmp = query(0,tot,L,R,1) + 1;
ret = max(ret,tmp);
update(0,tot,pos,tmp,1);
}
printf("%d\n",ret);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: