H的范围是10^15,DP方程很容易想到。但是因为H的范围太大了,而n的范围还算可以接受。因此,对高度排序排重后。使用新的索引建立线段树,使用线段树查询当前高度区间内的最大值,以及该最大值的前趋索引。线段树中的结点索引一定满足i<j的条件,因为采用从n向1更新线段树结点。每次线段树查询操作就可以得到argmax(dp[L, R]),很据不等式很容易得到L和R的范围。

 /* 474E */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std; #define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
__int64 mx;
int fa;
} node_t; const int maxn = 1e5+;
node_t nd[maxn<<];
__int64 h[maxn], hh[maxn];
int fa[maxn], dp[maxn]; void PushUp(int rt) {
int lb = rt<<;
int rb = rt<<|; if (nd[lb].mx >= nd[rb].mx) {
nd[rt].fa = nd[lb].fa;
nd[rt].mx = nd[lb].mx;
} else {
nd[rt].fa = nd[rb].fa;
nd[rt].mx = nd[rb].mx;
}
} void build(int l, int r, int rt) {
nd[rt].mx = -;
nd[rt].fa = ;
if (l == r)
return ;
int mid = (l+r)>>;
build(lson);
build(rson);
} void update(int x, int in, int l, int r, int rt) {
if (l == r) {
if (nd[rt].mx < dp[in]) {
nd[rt].mx = dp[in];
nd[rt].fa = in;
}
return ;
}
int mid = (l+r)>>;
if (x <= mid)
update(x, in, lson);
else
update(x, in, rson);
PushUp(rt);
} node_t query(int L, int R, int l, int r, int rt) {
node_t d;
if (L<=l && R>=r)
return nd[rt];
int mid = (l+r)>>;
if (R <= mid) {
return query(L, R, lson);
} else if (L > mid) {
return query(L, R, rson);
} else {
node_t ln = query(L, R, lson);
node_t rn = query(L, R, rson);
return rn.mx>ln.mx ? rn:ln;
}
} int main() {
int i, j, k;
int n, m, d;
int ans, v;
int l, r;
node_t node;
__int64 tmp; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d %d", &n, &d);
for (i=; i<=n; ++i) {
scanf("%I64d", &h[i]);
hh[i] = h[i];
}
sort(h+, h++n);
m = unique(h+, h++n) - (h+);
build(, m, ); for (i=n; i>; --i) {
dp[i] = ;
fa[i] = ; tmp = hh[i] + d;
if (tmp <= h[m]) {
l = lower_bound(h+, h++m, tmp) - h;
node = query(l, m, , m, );
if (node.mx+ > dp[i]) {
dp[i] = node.mx + ;
fa[i] = node.fa;
}
} tmp = hh[i] - d;
if (tmp >= h[]) {
r = m+;
if (tmp < h[m])
r = upper_bound(h+, h++m, tmp) - h;
node = query(, r-, , m, );
if (node.mx+ > dp[i]) {
dp[i] = node.mx + ;
fa[i] = node.fa;
}
} l = lower_bound(h+, h++m, hh[i]) - h;
update(l, i, , m, );
} ans = dp[];
v = ;
for (i=; i<=n; ++i) {
if (dp[i] > ans) {
ans = dp[i];
v = i;
}
} printf("%d\n", ans);
printf("%d", v);
while (fa[v]) {
v = fa[v];
printf(" %d", v);
}
putchar('\n'); #ifndef ONLINE_JUDGE
printf("%d\n", (int)clock());
#endif return ;
}

【CF】474E Pillars的更多相关文章

  1. 【CF】438E. The Child and Binary Tree

    http://codeforces.com/contest/438/problem/E 题意:询问每个点权值在 $c_1, c_2, ..., c_m$ 中,总权值和为 $s$ 的二叉树个数.请给出每 ...

  2. 【CF】148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题意:w个白b个黑,公主和龙轮流取,公主先取,等概率取到一个.当龙取完后,会等概率跳出一只.(0<= ...

  3. 【CF】328 D. Super M

    这种图论题已经变得简单了... /* D */ #include <iostream> #include <string> #include <map> #incl ...

  4. 【CF】323 Div2. D. Once Again...

    挺有意思的一道题目.考虑长度为n的数组,重复n次,可以得到n*n的最长上升子序列.同理,也可以得到n*n的最长下降子序列.因此,把t分成prefix(上升子序列) + cycle(one intege ...

  5. 【CF】7 Beta Round D. Palindrome Degree

    manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...

  6. 【CF】86 B. Petr#

    误以为是求满足条件的substring总数(解法是KMP分别以Sbeg和Send作为模式串求解满足条件的position,然后O(n^2)或者O(nlgn)求解).后来发现是求set(all vali ...

  7. 【CF】121 Div.1 C. Fools and Roads

    题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...

  8. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

  9. 【CF】110 Div.1 B. Suspects

    这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...

随机推荐

  1. shell中exit命令不退出脚本

    好久不用shell了,今天碰到一个坑,发现exit后,shell脚本还会运行. $JAVA_HOME/bin/jps | while read RES do PID=`echo $RES | awk ...

  2. ASP.NET操作DataTable

    .创建 datatable DataTable dt=new Datable();//可以给表创建一个名字,tb .给表加个列名: dt.Columns.Add("id", typ ...

  3. Python学习入门教程,字符串函数扩充详解

    因有用户反映,在基础文章对字符串函数的讲解太过少,故写一篇文章详细讲解一下常用字符串函数.本文章是对:程序员带你十天快速入门Python,玩转电脑软件开发(三)中字符串函数的详解与扩充. 如果您想学习 ...

  4. tomcat中jsp编译

    tomcat运行的工程中,jsp替换文件后可能不起作用.原因是jsp也是需要编译的.编译后的文件存放在tomcat/work文件夹下.如果替换不起作用,可以将work文件夹下的内容删除掉,重新启tom ...

  5. javascript 【js‘s word】

    http://mp.weixin.qq.com/s?__biz=MjM5MzY2NDY0Ng==&mid=214013689&idx=1&sn=21e03f6c7bf73893 ...

  6. 使用本地光盘安装Microsoft .NET Framework 3.5 for Win8/WinServer2012

    如何使用本地源安装Microsoft .NET Framework 3.5 作为SQL Server 2012的 必要组件,校验组件过程有个小BUG,即使没有安装也通过,但会卡在安装环节(enabli ...

  7. EditPlus 快捷键

    FileFtpUpload       Ctrl+Shift+S       上传文件到 FTP 服务器 FileNew           Ctrl+N           新建普通的文本文档 Fi ...

  8. [转]toString()方法

    文章转自:http://blog.sina.com.cn/s/blog_85c1dc100101bxgg.html 今天看JS学习资料,看到一个toString()方法,在JS中,定义的所有对象都具有 ...

  9. SGU 296.Sasha vs. Kate(贪心)

    题意: 给出长度为n(<=1000)的一个数.输出删掉k个数字后的最大值. Solution: 简单贪心. s[i]代表数字s的第i位. 从前往后第一个满足s[i]>s[i-1]的位置,最 ...

  10. Linux 信号量同步编程

    前一篇文章概述了Linux 系统中信号量互斥编程,这篇文章正好是前一篇的姊妹篇----信号量同步.说它们是姊妹篇是因为它们都是利用了内核的信号量机制实现了进程间的通信.因为两者所解决的问题不同,因此它 ...