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 ...
随机推荐
- .net core创建项目(指令方式)
所谓的指令创建项目,就是不用再已安装的VS2015的环境下或者VS Core下创建,直接通过DOS指令创建也是OK的. 1.找到你所准备保存项目的项目文件夹(你也可以到某个目录用指令创建项目文件夹[ ...
- How to use CAR FANS C800 Diagnostic Scan Tool to do diagnosis operation
How to use Heavy Duty Diagnostic CAR FANS C800 Diagnostic Scan Tool to do diagnosis operation Here i ...
- PDF文档导出
代码如下: /// <summary> /// 获取html内容,转成PDF(注册) /// </summary> public void DownloadPDFByHTML( ...
- spark读取hbase形成RDD,存入hive或者spark_sql分析
object SaprkReadHbase { var total:Int = 0 def main(args: Array[String]) { val spark = SparkSession . ...
- The Little Prince-12/04
The Little Prince-12/04 The wheat fields have nothing to say to me. And that is sad. But you have ha ...
- v-text v-html等指令的使用
v-text:以纯文本方式显示数据: v-html:可以识别HTML标签: v-once:只渲染元素或组件一次: v-pre:不进行编译,直接显示内容: v-cloak:可以隐藏未编译的 Mustac ...
- Mysql报错java.sql.SQLException:null,message from server:"Host '27,45,38,132' is not allowed to connect
Mysql报错java.sql.SQLException:null,message from server:"Host '27,45,38,132' is not allowed to co ...
- sync—WaitGroup
用途:阻塞主线程的执行,直到所有的goroutine执行完成 WaitGroup总共有三个方法:Add(delta int),Done(),Wait().简单的说一下这三个方法的作用. Add:添加或 ...
- div容器中内容垂直居中
#box{ width:200px; height:200px; line-height: 200px; vertical-align: middle; margin: 5px; background ...
- RHEL6/7 x86_64下cachefilesd占用cpu达到100%
昨天,有个测试环境cachedfilesd CPU 100%,一直在跑了挺久,经查 1. CacheFiles介绍NFS是一种经常使用到的网络共享文件系统,在分布式环境下,多台服务器的文件共享是一个问 ...