/****************************** * author :crazy_石头 * data structure: 线段树 * created time:2013/11/5 11:27 * Pro:ZOJ3349-线段树+dp * Judge Status:Accepted * Memory:3008K * Time:510MS *******************************/ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; #define rep(i,h,n) for(int i=(h);i<=(n);i++) #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define ll __int64 const int maxn=100000+5; int sum[maxn<<2],num[maxn],a[maxn]; int dp[maxn]; inline int max(int a,int b) { return a>b?a:b; } inline void pushup(int rt) { sum[rt]=max(sum[rt<<1],sum[rt<<1|1]); } inline void build(int l,int r,int rt) { if(l==r) { sum[rt]=0; return ; } int mid=(l+r)>>1; build(lson); build(rson); pushup(rt); } inline void update(int l,int r,int rt,int pos,int value) { if(l==r) { sum[rt]=value; return ; } int mid=(l+r)>>1; if(pos<=mid) update(lson,pos,value); else update(rson,pos,value); pushup(rt); } inline int query(int l,int r,int rt,int L,int R) { if(L<=l&&r<=R) { return sum[rt]; } int mid=(l+r)>>1; int ret=-1; if(L<=mid) ret=max(query(lson,L,R),ret); if(R>mid) ret=max(query(rson,L,R),ret); return ret; } int main() { int n,d; while(scanf("%d%d",&n,&d)!=EOF) { rep(i,1,n) { dp[1]=1; scanf("%d",&num[i]); a[i]=num[i]; } build(1,n,1); sort(a+1,a+n+1); int all=unique(a+1,a+n+1)-(a+1); rep(i,1,n) { int rt=lower_bound(a+1,a+all+1,num[i])-a; int l=lower_bound(a+1,a+all+1,num[i]-d)-a; int r=upper_bound(a+1,a+all+1,num[i]+d)-a-1; dp[i]=query(1,n,1,l,r)+1; update(1,n,1,rt,dp[i]); } int res=-1; rep(i,1,n) res=max(res,dp[i]); printf("%d\n",res); } return 0; } |