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 ...
随机推荐
- 基于CentOS6.5下snort+barnyard2+base的入侵检测系统的搭建(图文详解)(博主推荐)
为什么,要写这篇论文? 是因为,目前科研的我,正值研三,致力于网络安全.大数据.机器学习研究领域! 论文方向的需要,同时不局限于真实物理环境机器实验室的攻防环境.也不局限于真实物理机器环境实验室的大数 ...
- PKU_campus_2018_D Chocolate
思路: 题目链接http://poj.openjudge.cn/practice/C18D/ kruskal过程中使用乘法原理计数. 实现: #include <bits/stdc++.h> ...
- python绘图工具包 matplotlib 中文乱码问题
环境: python2.7 windows 8.1 解决: 改配置文件,把字体改为支持中文的字体. 找到python安装目录下的 \Lib\site-packages\matplotlib\mpl-d ...
- spring mvc 解决 Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml] 异常
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro ...
- 【HEVC帧间预测论文】P1.1 基于运动特征的HEVC快速帧间预测算法
基于运动特征的 HEVC 快速帧间预测算法/Fast Inter-Frame Prediction Algorithm for HEVC Based on Motion Features <HE ...
- TensorFlow 安装 Win10 Python+GPU
前叙:有灵魂的程序都是每一个程序员的最终目标.TensorFlow了解下? 打算花几个月学机器学习,TensorFlow是很好的选择,折腾了会环境,略有心得分享下. 环境:win10 Python:3 ...
- SQLite – LIMIT子句
SQLite - LIMIT子句 SQLite LIMIT子句是用来限制SELECT语句返回的数据量. 语法: SELECT语句.LIMIT子句的基本语法如下: SELECT column1, col ...
- python中*号和**号的用法
1.乘法符号 2.可变长参数 当我们使用函数时,需要传入不定个数的位置参数时,就可以使用*号表示,即*args,以元组形式传入:需要传入不定个数的关键字参数时,使用**表示,即**kwargs,以字典 ...
- Python3简明教程(四)—— 流程控制之分支
我们通过 if-else 语句来做决定,来改变程序运行的流程. if语句 语法如下: if expression: do this 如果表达式 expression 的值为真(不为零的任何值都为真), ...
- powerDesigner的name和comment转化
name2comment.vbs '****************************************************************************** '* ...