题目地址:http://codeforces.com/contest/474/problem/E

第一次遇到这样的用线段树来维护DP的题目。ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是那题有简单方法,于是就没写。这次最终写出来了。。

这题的DP思想跟求最长上升子序列的思想是一样的。仅仅只是这里的找前面最大值时会超时,所以能够用线段树来维护这个最大值,然后因为还要输出路径,所以要用线段树再来维护一个每一个数在序列中所在的位置信息。

手残了好多地方,最终调试出来了。。。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define LL __int64
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
const int INF=0x3f3f3f3f;
const int MAXN=100000;
int maxv[MAXN<<2], cnt, pre[MAXN+10], f[MAXN+10], q_maxp, maxp[MAXN<<2], q_maxv;
LL a[MAXN+10], c[MAXN+10], d[MAXN+10];
void PushUp(int rt)
{
maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
if(maxv[rt<<1]>=maxv[rt<<1|1])
maxp[rt]=maxp[rt<<1];
else
maxp[rt]=maxp[rt<<1|1];
}
void update(int p, int x, int i, int l, int r, int rt)
{
if(l==r)
{
maxv[rt]=x;
maxp[rt]=i;
return ;
}
int mid=l+r>>1;
if(p<=mid) update(p,x,i,lson);
else update(p,x,i,rson);
PushUp(rt);
}
void query(int ll, int rr, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
if(q_maxv<maxv[rt])
{
q_maxv=maxv[rt];
q_maxp=maxp[rt];
}
return ;
}
int mid=l+r>>1, ans=0;
if(ll<=mid) query(ll,rr,lson);
if(rr>mid) query(ll,rr,rson);
}
int bin_seach(LL x)
{
int low=0, high=cnt-1, mid;
while(low<=high)
{
mid=low+high>>1;
if(d[mid]==x) return mid;
else if(d[mid]>x) high=mid-1;
else low=mid+1;
}
}
int l_seach(LL x)
{
int low=0, high=cnt-1, mid, ans=-1;
while(low<=high)
{
mid=low+high>>1;
if(d[mid]<=x)
{
ans=mid;
low=mid+1;
}
else high=mid-1;
}
return ans;
}
int r_seach(LL x)
{
int low=0, high=cnt-1, mid, ans=-1;
while(low<=high)
{
mid=low+high>>1;
if(d[mid]>=x)
{
ans=mid;
high=mid-1;
}
else low=mid+1;
}
return ans;
}
void print(int x)
{
if(x==-1) return ;
print(pre[x]);
printf("%d ",x+1);
}
int main()
{
int n, dd, i, x, ans, y, z, max1=-1, pos, tot;
scanf("%d%d",&n,&dd);
for(i=0; i<n; i++)
{
scanf("%I64d",&a[i]);
c[i]=a[i];
}
sort(c,c+n);
d[0]=c[0];
cnt=1;
for(i=1; i<n; i++)
{
if(c[i]!=c[i-1])
{
d[cnt++]=c[i];
}
}
/*for(i=0;i<cnt;i++)
{
printf("%d ",c[i]);
}
puts("");*/
memset(maxv,0,sizeof(maxv));
memset(pre,-1,sizeof(pre));
for(i=0; i<n; i++)
{
x=bin_seach(a[i]);
y=l_seach(a[i]-dd);
z=r_seach(a[i]+dd);
//printf("%d %d %d\n",x,y,z);
q_maxp=-1;
q_maxv=-1;
if(y!=-1)
query(0,y,0,cnt-1,1);
if(z!=-1)
query(z,cnt-1,0,cnt-1,1);
update(x,q_maxv+1,i,0,cnt-1,1);
pre[i]=q_maxp;
if(q_maxv==0)
pre[i]=-1;
if(max1<q_maxv+1)
{
max1=q_maxv+1;
pos=i;
}
}
printf("%d\n",max1);
print(pos);
return 0;
}

Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)的更多相关文章

  1. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. Codeforces Round #305 (Div. 2) D题 (线段树+RMQ)

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  4. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

  5. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  6. Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并

    D. Developing Game   Pavel is going to make a game of his dream. However, he knows that he can't mak ...

  7. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  8. Codeforces Round #546 (Div. 2) E 推公式 + 线段树

    https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...

  9. Codeforces Round #275 Div.1 B Interesting Array --线段树

    题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...

随机推荐

  1. erlang集群IP及port管理

    erlang集群是依靠epmd维护的,epmd是erlang集群节点间port映射的守护进程.负责维护集群内的节点连接.提供节点名称到IP地址及port的解析服务. epmd 自己定义port号 ep ...

  2. 第1周 SQL Server 如何执行一个查询

    原文:第1周 SQL Server 如何执行一个查询 大家好,欢迎来到第1周的SQL Server性能调优培训.在我们进入SQL Server性能调优里枯燥难懂的细节内容之前,我想通过讲解SQL Se ...

  3. IIS6,IIS7中查看w3wp进程

    当我们服务器创建了很多应用程序池,然后某个w3wp.exe进程占用CPU和内存过高,我们怎么查找这个w3wp.exe进程属于哪一个网站呢.其实微软为我们提供了很好的查看工具: 首先打开windows任 ...

  4. appendChild的用法

    appendChild的用法 1,先把元素从原有父级上删除 2,再把元素添加到新父级上

  5. 使用BackgroundWorker组件进行异步操作编程

    本文介绍了BackgroundWorker组件的功能及在基于事件的异步操作编程中的应用,并对组件的实现原理进行简述.在应用程序中,可能会遇到一些执行耗时的功能操作,比如数据下载.复杂计算及数据库事务等 ...

  6. 当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com

    如图所示, 码,如以下:<input id="user_sn" class="loginInput" name="user_sn" t ...

  7. OWIN编写中间件

    OWIN系列之自己动手编写中间件 一.前言 1.基于OWIN的项目摆脱System.Web束缚脱颖而出,轻量级+跨平台,使得ASP.NET应用程序只需依赖这个抽象接口,不用关心所运行的Web服务器. ...

  8. BZOJ 1324 Exca神剑 最小割

    标题效果:给定一个n*m矩阵.所有的格宝石之子,人们可选择起始位置,后除去宝石的当前位置的周围消失,然后你就可以走两步,重复上述过程 easy发现格儿子把它周围格孩子不能拿 因此,党格访问问题 黑白染 ...

  9. 【Nginx】磁盘文件写入飞地发

    文章继续.什么时候Nginx当用户请求一个文件,这将无法读取该文件的内容加载到内存,然后从内存发送,但电话sendfile况下,从内核直接发送出去.这样做显然效率要更高.Nginx也为我们封装好了一系 ...

  10. Ini文件帮助类

    .ini文件是什么 .ini 文件是Initialization File的缩写,就是初始化文件.在Windows系统中,其是配置文件所采用的存储格式(主要是system.ini,win.ini,sy ...