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 ...
随机推荐
- 文本tfidf
文本分类tf:词的频率 idf:逆文档频率 代码实例: # tf idf from sklearn.feature_extraction.text import TfidfVectorizer imp ...
- python+selenium win32gui实现文件上传 enumerate()
upload = dr.find_element_by_id('exampleInputFile0') upload.click() time.sleep(1) # win32gui dialog = ...
- oj练习---dp专题
1.POJ 3744 Scout YYF I 经典的dp模型,但是要用到快速矩阵幂加速,分段的思想 # include <stdio.h> # include <algorithm& ...
- Android NDK MediaCodec在ijkplayer中的实践
https://www.jianshu.com/p/41d3147a5e07 从API 21(Android 5.0)开始Android提供C层的NDK MediaCodec的接口. Java Med ...
- awk中截取IP字段
由于文本的特殊性,IP字段可能并不是在特定的字段中. 借助awk的match()函数进行匹配截取 awk --re-interval '($0 ~ "xxx"){match($0, ...
- springboot 事务回滚
在springboot中,使用事务回滚时,添加@Transactional注解,然后在try-catch块中,发生异常时,在catch中 添加 TransactionAspectSupport.cur ...
- NOI 2007 货币兑换Cash (bzoj 1492) - 斜率优化 - 动态规划 - CDQ分治
Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和 B纪念券(以下 简称B券).每个持有金券的顾客都有一个自己的帐户.金券的数目可以是一个 ...
- Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力
Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...
- Codeforces 799D Field expansion - 搜索 - 贪心
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game p ...
- CSS的进一步深入(更新中···)
在之前我们学了6种选择器和三种CSS样式的引入,学习选择器就是为了更好的选择文本,学习CSS的引入是为了使文本增加各种样式和属性, 下面我们简单来学习一下为文本加样式和一些属性和属性值: 1.文本的样 ...