codeforces 271E 离散化+线段树,dp优化

E. Pillars
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Marmot found a row with n pillars. The i-th pillar has the height of hi meters. Starting from one pillar i1, Marmot wants to jump on the pillars i2, ..., ik. (1 ≤ i1 < i2 < ... < ik ≤ n). From a pillar i Marmot can jump on a pillar j only if i < j and |hi - hj| ≥ d, where |x| is the absolute value of the number x.

Now Marmot is asking you find out a jump sequence with maximal length and print it.

Input

The first line contains two integers n and d (1 ≤ n ≤ 1050 ≤ d ≤ 109).

The second line contains n numbers h1, h2, ..., hn (1 ≤ hi ≤ 1015).

Output

The first line should contain one integer k, the maximal length of a jump sequence.

The second line should contain k integers i1, i2, ..., ik (1 ≤ i1 < i2 < ... < ik ≤ n), representing the pillars' indices from the maximal length jump sequence.

If there is more than one maximal length jump sequence, print any.

Sample test(s)
input
5 2
1 3 6 7 4
output
4
1 2 3 5 
input
10 3
2 1 3 6 9 11 7 3 20 18
output
6
1 4 6 7 8 9 
Note

In the first example Marmot chooses the pillars 1235 with the heights 1364. Another jump sequence of length 4 is 1245.


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
//typedef pair pii; 
#define INF 1e16
#define MAXN 100005
#define MAXM 100
const int maxn = 100005;
const int mod = 1000000007 ;
#define eps 1e-6
#define PI 3.1415926535897932384626433
#define rep(i,n) for(int i=0;i pii;
#define mk(n,m) make_pair(n,m)
#define lson l,m,rt<<1  
#define rson m+1,r,rt<<1|1
ll h[maxn],val[maxn];
int dp[maxn];
int pre[maxn];
pii Max[maxn<<2];
void pushup(int rt)
{
	if(Max[rt<<1].first <= Max[rt<<1|1].first)
		Max[rt] = Max[rt<<1|1];
	else
		Max[rt] = Max[rt<<1];
}
void update(int L,int R,int c,int ps,int l,int r,int rt)
{
	if (L <= l && r <= R)  
	{
		Max[rt] = mk(c,ps);
		return;
	}
	int m = (l+r)>>1;
	if(L <= m) update(L,R,c,ps,lson);
	if(R > m) update(L,R,c,ps,rson);
	pushup(rt);
}
pii query(int L,int R,int l,int r,int rt)
{
	if( R < L) return mk(0,0);
	if (L <= l && r <= R)  
	{
		return Max[rt];
	}
	int m = (l+r)>>1;
	pii ret = mk(0,0);
	if(L <= m) ret = max(ret,query(L,R,lson));
	if(R > m) ret = max(ret, query(L,R,rson));
	return ret;
}
int n,d;
vector ans;
int main()
{
#ifndef ONLINE_JUDGE  
	freopen("in.txt","r",stdin);  
	//   freopen("out.txt","w",stdout);  
#endif
	while(~scanf("%d%d",&n,&d))
	{
		ini(Max);
		ini(dp);
		ini(pre);
		rep1(i,n){
			scanf("%I64d",&h[i]);
			val[i] = h[i];
		}
		sort(val+1,val+1+n);
		int V = unique(val+1,val+1+n) - val;
		rep1(i,n)
		{
			int l = upper_bound(val+1,val+1+V,h[i]-d) - val - 1;
			int r = lower_bound(val+1,val+1+V,h[i]+d) - val;
			pii ret = max(query(1,l,1,V,1),query(r,V,1,V,1));
			dp[i] = ret.first + 1;
			pre[i] = ret.second;
			int pos = lower_bound(val+1,val+1+V,h[i]) - val;
			update(pos,pos,dp[i],i,1,V,1);
		}
		int mx = 0, mxid;
		rep1(i,n)
		{
			if(mx < dp[i])
			{
				mx = dp[i];
				mxid = i;
			}
		}
		cout<=0;i--)
		{
			printf("%d%c",ans[i],i==0?'\n':' ');
		}
	}
	return 0;
} 


你可能感兴趣的:(dp,线段树)