Portal

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\)结尾的满足要求的序列长度,则:

\[ f[i]=max\{f[j]\}+1 \quad (j\leq i-d \vee j\geq i+d) $$因为$a_i$比较大,所以先离散化一波。离散化之后求$j\leq i-d \vee j\geq i+d$时,二分一下即可,不会增加复杂度。可以用树状数组来维护$f[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的更多相关文章

  1. Codeforces 474 E. Pillars

    水太...... E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Grains 与 Pillars

    Grains 与 Pillars Grains介绍 Grains接口是salt用来采集底层系统信息的,包含了操作系统信息.域名.IP地址.内核.内存等一些底层信息.就是因为grains采集了这些信息, ...

  3. 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 ...

  4. 【BZOJ 4148】 4148: [AMPPZ2014]Pillars (乱搞)

    4148: [AMPPZ2014]Pillars Time Limit: 5 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 100  Solve ...

  5. [CF 474E] Pillars (线段树+dp)

    题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...

  6. 【CF】474E Pillars

    H的范围是10^15,DP方程很容易想到.但是因为H的范围太大了,而n的范围还算可以接受.因此,对高度排序排重后.使用新的索引建立线段树,使用线段树查询当前高度区间内的最大值,以及该最大值的前趋索引. ...

  7. Codeforces 474E - Pillars

    一眼看上去非常像最长不下降子序列. 然后比赛的时候对每个答案长度为k的序列,维护最后一个数的最大值和最小值. 当时不知道为什么认为从长度最长倒推至前面不会太长,于是心满意足地敲了个O(n^2).结果T ...

  8. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  9. html5 Websockets development guidance

    1. WebSockets -- full-duplex communication The main HTML5 pillars include Markup, CSS3, and JavaScri ...

随机推荐

  1. Linux上不了网——wget无法解析主机

    很有可能是网关和域名服务器没有设置 1.设置网关 netstat -rn #查看网关配置情况 [hadoop@slave1 ~]$ route -n Kernel IP routing table D ...

  2. AJPFX浅谈关于Java程序员缺乏面向对象的基本功的问题

    为什么很多 Java 程序员会缺乏面向对象基本功?这得怪那些 Java 框架.现在 Java 的各种框架太发达.太傻瓜化了,导致很多程序员只需要按部就班.照着框架进行代码填空,基本已经丧失了 OOA ...

  3. 基于socketserver实现的并发(tcp和udp)

    threading 线程 基于tcp协议:请求建立连接,然后开启进程 基于udp协议:直接开启新进程 基于tcp协议 import socketserver # 导入socketserver模块 # ...

  4. 全志R58平台的GPIO引脚控制

    全志R58平台的GPIO引脚控制 2017/8/18 15:50 版本:V1.0 开发板:SC5806(全志R58平台) SDK:android4.4.4 本文以GPIO引脚PD24为例,在开发板的背 ...

  5. EEPROM介绍

    EEPROM( Electrically Erasable Programmable Read Only Memory )全称是电气可擦除可编程只读存储器,是非易失存储器,可以访问到每个字节,容量比较 ...

  6. SQLite运算符

    SQLite运算符 SQLite的运算符是什么? 运算符是一个保留字或一个字符主要用于SQLite语句的WHERE子句来执行操作,如比较和算术运算. 操作符用于指定条件的SQLite语句和作为连词在一 ...

  7. Oracle中的for和while循环

    实例: beginfor i in 51..500 loop delete from test t where t.date=to_date('2016-07-01', 'yyyy-MM-dd') a ...

  8. 在ios中使用FMDB

    SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iOS SDK很早就支持了SQLite,在使用时,只需要加入 libsqlite3.dyli ...

  9. JS 手机号中间4位变星号

    一:正则方法 var str1 = '13991367972'var reg = /^(\d{3})\d*(\d{4})$/;var str2 = str1.replace(reg,'$1****$2 ...

  10. 洛谷——P1627 [CQOI2009]中位数

    P1627 [CQOI2009]中位数 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 中位数的题目有关统计的话,可以转 ...