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 ...
随机推荐
- 机器学习概念之特征选择(Feature selection)之VectorSlicer算法介绍
不多说,直接上干货! VectorSlicer 算法介绍: VectorSlicer是一个转换器,输入特征向量,输出原始特征向量子集.VectorSlicer接收带有特定索引的向量列,通过对这些索引的 ...
- 项目错误提示Multiple markers at this line
新安装个Myeclipse,导入以前做的程序后程序里好多错,第一行提示: Multiple markers at this line - The type java.lang.Obje ...
- Asp.net 字符(一)
1.字母大小写处理 private string GetChangedStr(string oldStr, strType type) { string newStr = ""; ...
- mysql对库,表及记录的增删改查
破解密码 #1.关闭mysqlnet stop mysqlmysql还在运行时需要输入命令关闭,也可以手动去服务关闭 #2.重新启动mysqld --skip-grant-tables跳过权限 #3m ...
- wkWebView 的一些问题
导语 WKWebView 是苹果在 WWDC 2014 上推出的新一代 webView 组件,用以替代 UIKit 中笨重难用.内存泄漏的 UIWebView.WKWebView 拥有60fps滚动刷 ...
- Android源码分析笔记--Handler机制
#Handler机制# Handler机制实际就是实现一个 异步消息循环处理器 Handler的真正意义: 异步处理 Handler机制的整体表述: 消息处理线程: 在Handler机制中,异步消息处 ...
- guanbi selinux
编辑/etc/sysconfig/selinux,把第一条选项改为 disabled
- Excel数据导入SQL Server
基本有2种方案,都是无需安装Office的方案 Ole DB读取 + BulkCopy 获取Excel各个SheetName //连接串 string strConn = "Provider ...
- 用户授权policy
定义策略类 php artisan make:policy PostPolicy app/Policies/PostPolicy.php public function update(User $us ...
- Python学前基础知识
Python基础计算机常识:硬件性能:CPU.内存输入设备:鼠标.键盘外部存储设备:硬盘输出设备;显示器.打印机(不算自带)通讯设备:无线网卡----------------------------- ...