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 <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
//typedef pair<int,int> 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<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(n) scanf("%d",&n)
#define scan2(n,m) scanf("%d%d",&n,&m)
#define scans(s) scanf("%s",s);
#define ini(a) memset(a,0,sizeof(a))
#define FILL(a,n) fill(a,a+maxn,n)
#define out(n) printf("%d\n",n)
//ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b);}
using namespace std;
typedef pair<int,int> 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<int> 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<<mx<<endl;
		ans.clear();
		ans.push_back(mxid);
		while(pre[mxid] != 0)
		{
			ans.push_back(pre[mxid]);
			mxid = pre[mxid];
		}
		for(int i=mx-1;i>=0;i--)
		{
			printf("%d%c",ans[i],i==0?'\n':' ');
		}
	}
	return 0;
} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值