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. RedHat7 Git 安装使用

    Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势. 搭建git环境 第一步: 安装Git # yum -y install git 第 ...

  2. document library\ picture library\Asset Library的默认文件夹

    document library\ picture library\Asset Library的默认文件夹? document library 默认文件夹:Forms picture library默 ...

  3. [Cookie] C#CookieHelper--C#操作Cookie的帮助类 (转载)

    点击下载 CookieHelper.rar 下面是代码大家看一下 /// <summary> /// 类说明:CookieHelper /// 联系方式:361983679 /// 更新网 ...

  4. 黑马程序员-File类+递归的简单应用

    Java File类   Java.io.File Java文件类以抽象的方式代表文件名和目录路径名.该类主要用于文件和目录的创建.文件的查找和文件的删除等. File对象代表磁盘中实际存在的文件和目 ...

  5. sql server split函数

    --创建分割函数CREATE FUNCTION dbo.Split(@String varchar(8000),@Delimiter char(1))returns @temptable TABLE ...

  6. C#之垃圾回收

    垃圾回收时现代语言的标志之一.垃圾回收解放了手工管理对象释放的工作,提高了程序的健壮性,但是副作用就是程序代码可以对于创建对象变得随意. 1.避免不必要的对象创建 由于垃圾回收的代价较高,所以C#程序 ...

  7. setTimeout 和 setInterval区别

    setTimeout和setIntelval都有定时的功能!!!取消定时功能的时候,都有对应的clearTimeout以及clearInterval与之对应. 但是他们之间是有区别的! setTime ...

  8. Javascript面试题浅析

    分享几道JavaScript相关的面试题. 字符串反转 这这里提供了两种解题思路.如果各位读者还有其他的思路,可以分享交流! 第一方法: function reverse(str){ var sp = ...

  9. 半质数的个数 csdn 英雄会 高校俱乐部

    2·14 情人&元宵节专题:半质数的个数. 题目:质数是大家熟知的概念,我们定义一个半质数的概念:如果一个数恰好是两个质数的乘积(可以相同),则称它为半质数.前几个半质数是 4, 6, 9, ...

  10. cocos2dx arpg单机手游

    这只是一个DEMO. ARPG 单机手游, 个人DEMO. 支持剧情编辑, 支持气泡对话, 支持人物图像对话, 支持随时角色切换, 支持NPC跟随, 共同作战, 支持LUA扩展, 支持BUFF技能, ...