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 ≤ 105, 0 ≤ 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.

Examples
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 1, 2, 3, 5 with the heights 1, 3, 6, 4. Another jump sequence of length 4 is 1, 2, 4, 5.

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=2e6+,inf=1e9+,mod=1e9+;
const LL INF=1e18+; struct SGT
{
int maxx[N<<];
void build(int l,int r,int pos)
{
memset(maxx,,sizeof(maxx));
}
void update(int p,int c,int l,int r,int pos)
{
if(l==r)
{
maxx[pos]=c;
return;
}
int mid=(l+r)>>;
if(p<=mid)update(p,c,l,mid,pos<<);
else update(p,c,mid+,r,pos<<|);
maxx[pos]=max(maxx[pos<<],maxx[pos<<|]);
}
int query(int L,int R,int l,int r,int pos)
{
if(L<=l&&r<=R)return maxx[pos];
int mid=(l+r)>>;
int ans=;
if(L<=mid)ans=max(ans,query(L,R,l,mid,pos<<));
if(R>mid)ans=max(ans,query(L,R,mid+,r,pos<<|));
return ans;
}
}tree;
LL a[N],b[N];
int n;
LL k;
int big(LL x)
{
int pos=lower_bound(b,b++n,x)-b;
return pos;
}
int low(LL x)
{
int pos=upper_bound(b,b++n,x)-b-;
return pos;
}
int dp[N];
vector<int>ans;
int main()
{
scanf("%d%lld",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
b[n+]=INF;
b[]=-INF;
for(int i=;i<=n;i++)
{
LL pre=a[i]-k;
LL nex=a[i]+k;
int pos1=low(pre);
int pos2=big(nex);
//cout<<pre<<" "<<nex<<" "<<pos1<<" "<<pos2<<endl;
int maxx=;
if(pos1>=)maxx=max(maxx,tree.query(,pos1,,n,));
if(pos2<=n)maxx=max(maxx,tree.query(pos2,n,,n,));
dp[i]=maxx+;
tree.update(low(a[i]),maxx+,,n,);
}
int maxx=,pre=-;
for(int i=;i<=n;i++)
{
if(dp[i]>maxx)
{
maxx=dp[i];
pre=i;
}
}
ans.push_back(pre);
for(int i=pre-;i>=;i--)
{
if(abs(a[i]-a[pre])>=k&&dp[pre]==dp[i]+)
{
pre=i;
ans.push_back(pre);
}
}
printf("%d\n",maxx);
for(int i=maxx-;i>=;i--)
printf("%d ",ans[i]);
return ;
}

Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp的更多相关文章

  1. Codeforces Round #278 (Div. 2) D. Strip 线段树优化dp

    D. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  2. Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...

  3. Codeforces Round #603 (Div. 2) E. Editor 线段树

    E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...

  4. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  5. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  6. Codeforces Round #765 Div.1 F. Souvenirs 线段树

    题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...

  7. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  8. Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  9. Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)

    Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...

随机推荐

  1. 前端框架VUE----webpack打包工具的使用

    在这里我仅仅的是对webpack做个讲解,webpack这个工具非常强大,解决了我们前端很繁琐的一些工具流程繁琐的事情.如果感兴趣的同学,还是看官网吧. 中文链接地址:https://www.webp ...

  2. Spring MVC 编程流程步骤

    Spring MVC 编程流程步骤 1. 建立Maven工程 2. 添加Spring MVC依赖 <dependencies> <dependency> <groupId ...

  3. Django高级

    一 登录装饰器 def login_required(view_func): '''登录判断装饰器''' def wrapper(request, *view_args, **view_kwargs) ...

  4. hibernate自动创建表报表不存在

    在hibernate.cfg.xml配置了<property name="hibernate.hbm2ddl.auto">update</property> ...

  5. FastStone Capture无法录制系统声音解决方法(win10)

    步骤一: 右键桌面-->个性化 步骤二: 主题-->高级声音设置 步骤三: 录音-->右键“立体声混音”,单击启用就OK了.

  6. Kattis之旅——Factovisors

    The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...

  7. 使用Selenium+firefox抓取网页指定firefox_profile后的问题

    from: https://blog.csdn.net/chufazhe/article/details/51145834 摘要:在使用selenium和firefox抓取网页指定firefox_pr ...

  8. python-selenium,关于页面滑动的操作

    //移动到元素element对象的“顶端”与当前窗口的“顶部”对齐 ((JavascriptExecutor) driver).executeScript("arguments[0].scr ...

  9. php+redis,延迟任务 实现自动取消订单,自动完成订单

    简单定时任务解决方案:使用redis的keyspace notifications(键失效后通知事件) 需要注意此功能是在redis 2.8版本以后推出的,因此你服务器上的reids最少要是2.8版本 ...

  10. 统计py文件中的代码行

    希望是输入一个合法的文件夹的路径,然后代码自动读取该文件夹下的每个py结尾的文件内的代码行数,最后汇总一个数,但现在只是有思路,却没时间写,这是能读取同级文件下的某个文件, with open('te ...