Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
1 second
256 megabytes
standard input
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.
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).
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.
5 2
1 3 6 7 4
4
1 2 3 5
10 3
2 1 3 6 9 11 7 3 20 18
6
1 4 6 7 8 9
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的更多相关文章
- 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 ...
- Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)
Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...
- 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 ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #765 Div.1 F. Souvenirs 线段树
题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- 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 ...
- 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 ...
随机推荐
- glog日志库移植Android平台
1.在linux平台下使用ndk交叉编译链编译glog生成libglog.a静态库. 2.将生成的库文件与头文件放到Android项目中,使用JNI方法调用. 3.编译遇到错误“stderr.stdo ...
- php开启xdebug扩展
1.下载Xdebug(先看php下的ext文件夹(C:\xampp\php\ext)下有没有php_xdebug.dll文件,如果有的话,就不用下了.) 到目前为止,Xdebug的最新版本为2.7.0 ...
- NGINX转发代理情况下,获取客户单真实IP
编译时加上http_realip_module 模块 realip模块生效的前提是:直接连接nginx的ip是在set_real_ip_from中指定的. 原机配置: set_real_ip_from ...
- Oracle 手动建库
Oracle在创建实例的时候,多数采用的是dbca的形式..其实手动建库可以提供更大的自由发挥的空间,根据情况进行定制 登录Oracle用户 指定SID(Instance Identifier) ex ...
- 2016 icpc ECfinal && codeforcesgym101194
一不小心惨变旅游队,不过上海的风景不错 顺带找其他队交流一下集训经验...或许可以成为选拔和集训16级的依据 A.直接模3就可以了,2^(3*n)%7=1 C.Mr. Panda and Strips ...
- Spring Boot 2(一):Spring Boot 2.0新特性
Spring Boot 2(一):Spring Boot 2.0新特性 Spring Boot依赖于Spring,而Spring Cloud又依赖于Spring Boot,因此Spring Boot2 ...
- zookeeper各种报错、原因及解决方法汇总(持续更新)
[root@iZ23nn1p4mjZ zookeeper-3.4.10]# bin/zkCli.sh -server localhost:2181,localhost:2182,localhost:2 ...
- 第一章-硬件组成及linux发展历史(1)
一.服务器与计算机的组成? 计算机组成主要有:CPU.硬盘.内存.电源.显示器.鼠标.键盘 服务器组成主要有:CPU.硬盘.内存.电源.RAID卡.远程控制卡 CPU: 即:中央处理器,是一块超大规模 ...
- 利用shell脚本通过ssh绕过输入密码直接登录主机
shell #!/usr/bin/expect spawn ssh root@192.168.137.141 expect "*password:" send "lizh ...
- [c/c++] programming之路(11)、顺序分支
一.模块化设计 #include<stdio.h> #include<stdlib.h> #include<windows.h> void openbaidu(){ ...