Codeforces474E - Pillars
Description
给出一个\(n(n\leq10^5)\)的正整数序列\(\{a_n\}(a_i\leq10^{15})\)和正整数\(d(d\leq10^9)\),求\(\{a_n\}\)的一个子序列\(\{b_m\}\),使得\(\forall i\in[1,m-1],|b_i-b_{i-1}|\geq d\)。
Solution
跟求最长上升子序列的方法差不多。\(f[i]\)表示目前以数值\(i\)结尾的满足要求的序列长度,则:
> 时间复杂度$O(nlogn)$。
##Code
```cpp
//Pillars
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long lint;
typedef std::pair<lint,int> pairI;
inline char gc()
{
static char now[1<<16],*s,*t;
if(s==t) {t=(s=now)+fread(now,1,1<<16,stdin); if(s==t) return EOF;}
return *s++;
}
inline lint read()
{
lint x=0; char ch=gc();
while(ch<'0'||'9'<ch) ch=gc();
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
return x;
}
int const N=2e5+10;
int n,d,n0; lint h[N],map[N];
int rt,cnt,ch[N][2]; pairI maxV[N];
void update(int p) {maxV[p]=max(maxV[ch[p][0]],maxV[ch[p][1]]);}
lint L,R;
void ins(int p,lint L0,lint R0,pairI x)
{
if(L==L0&&R0==L) {maxV[p]=x; return;}
for(int i=0;i<2;i++) if(!ch[p][i]) ch[p][i]=++cnt;
lint mid=L0+R0>>1;
if(L<=mid) ins(ch[p][0],L0,mid,x);
else ins(ch[p][1],mid+1,R0,x);
update(p);
}
pairI query(int p,lint L0,lint R0)
{
if(L<=L0&&R0<=R) return maxV[p];
for(int i=0;i<2;i++) if(!ch[p][i]) ch[p][i]=++cnt;
lint mid=L0+R0>>1; pairI r=pairI(0,0);
if(L<=mid) r=max(r,query(ch[p][0],L0,mid));
if(mid<R) r=max(r,query(ch[p][1],mid+1,R0));
return r;
}
int ans,seq[N],pre[N];
int main()
{
n=read(),d=read();
for(int i=1;i<=n;i++) map[i]=h[i]=read();
sort(map+1,map+n+1); n0=unique(map+1,map+n+1)-map-1;
for(int i=1;i<=n;i++) h[i]=lower_bound(map+1,map+n0+1,h[i])-map;
rt=++cnt;
for(int i=1;i<=n;i++)
{
int x=upper_bound(map+1,map+n0+1,map[h[i]]-d)-map-1;
int y=lower_bound(map+1,map+n0+1,map[h[i]]+d)-map;
int len=0; pairI t=pairI(0,0);
L=1,R=x; if(L<=R) t=query(rt,1,n0);
if(t.first>len) len=t.first,pre[i]=t.second;
L=y,R=n0; if(L<=R) t=query(rt,1,n0);
if(t.first>len) len=t.first,pre[i]=t.second;
L=h[i],ins(rt,1,n0,pairI(len+1,i));
}
L=1,R=n0; pairI t=query(rt,1,n0);
ans=t.first; printf("%d\n",ans);
for(int i=ans,x=t.second;i>=1;i--,x=pre[x]) seq[i]=x;
for(int i=1;i<=ans;i++) printf("%d ",seq[i]); puts("");
return 0;
}
```
##P.S.
老师留的那天忘记写了...真是怠惰啊
一开始懒到不想写离散化于是开了个$10^{15}$的动态开点线段树,结果`MLE`了。\]
Codeforces474E - Pillars的更多相关文章
- Codeforces 474 E. Pillars
水太...... E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Grains 与 Pillars
Grains 与 Pillars Grains介绍 Grains接口是salt用来采集底层系统信息的,包含了操作系统信息.域名.IP地址.内核.内存等一些底层信息.就是因为grains采集了这些信息, ...
- Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- 【BZOJ 4148】 4148: [AMPPZ2014]Pillars (乱搞)
4148: [AMPPZ2014]Pillars Time Limit: 5 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 100 Solve ...
- [CF 474E] Pillars (线段树+dp)
题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...
- 【CF】474E Pillars
H的范围是10^15,DP方程很容易想到.但是因为H的范围太大了,而n的范围还算可以接受.因此,对高度排序排重后.使用新的索引建立线段树,使用线段树查询当前高度区间内的最大值,以及该最大值的前趋索引. ...
- Codeforces 474E - Pillars
一眼看上去非常像最长不下降子序列. 然后比赛的时候对每个答案长度为k的序列,维护最后一个数的最大值和最小值. 当时不知道为什么认为从长度最长倒推至前面不会太长,于是心满意足地敲了个O(n^2).结果T ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- html5 Websockets development guidance
1. WebSockets -- full-duplex communication The main HTML5 pillars include Markup, CSS3, and JavaScri ...
随机推荐
- solr管理界面下统计多个时间段的数据 facet.query
在Raw Query Parameters参数里面输入时间段即可 如下图所示: facet.query=publishTime:[2017-06-05T00:00:00Z TO 2017-06-07T ...
- 十个非常棒的学习angularjs的英文网站
AngularJS 是非常棒的JS框架,能够创建功能强大,动态功能的Web app.AngularJS自2009发布以来,已经广泛应用于Web 开发中.但是对想要学习Angular JS 的人而言,只 ...
- [转]Android 如何监听返回键,弹出一个退出对话框
本文转自:http://blog.csdn.net/sunnyfans/article/details/8094349 Android 如何监听返回键点击事件,并创建一个退出对话框, 防止自己写的应用 ...
- 动手实现 React-redux(五):Provider
我们要把 context 相关的代码从所有业务组件中清除出去,现在的代码里面还有一个地方是被污染的.那就是 src/index.js 里面的 Index: ... class Index extend ...
- 动手实现 Redux(三):纯函数(Pure Function)简介
我们接下来会继续优化我们的 createStore 的模式,让它使我们的应用程序获得更好的性能. 但在开始之前,我们先用一节的课程来介绍一下一个函数式编程里面非常重要的概念 —— 纯函数(Pure F ...
- 浅析cookie
基本概念:cookie是指web浏览器存储的少量数据,该数据会在每次请求一个相关的URL时自动传到服务器中. 以博客园为例,我们看看cookie有哪些属性: 1.Name:cookie的名称: 2. ...
- ASP.NET MVC5的一个轻量级的框架学习的第一天
第二步第三部 这是第一天的小试成功,怪自己太笨了,一个错排查好久,还好有源码看着了解,后续还得多努力,
- How to proxy a web site by apache2 in Ubuntu
Install apache2 To execute the install command in terminal: sudo apt-get install apache2 Then, we ca ...
- Linux系统使用iftop查看带宽占用情况
Linux系统下如果服务器带宽跑满了,查看跟哪个ip通信占用带宽比较多,可以通过iftop命令进行查询,使用方法如下: 1 安装方法[软件官网地址:http://www.ex-parrot.com/~ ...
- Summary of 2016 International Trusted Computing and Cloud Security Summit
1) Welcome Remarks 2) The advancement of Cloud Computing and Tursted Computing national st ...