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

来源:互联网 发布:airplay镜像 windows 编辑:程序博客网 时间:2024/05/16 15:25

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 21 3 6 7 4
output
41 2 3 5 
input
10 32 1 3 6 9 11 7 3 20 18
output
61 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 100const 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|1ll 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];elseMax[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);  #endifwhile(~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 0
原创粉丝点击