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. Android开发进阶:如何读写Android文件

    Android主要有四大主要组件组成:Activity.ContentProvider.Service.Intent组成.Android文件的运行主要需要读写四大组件的文件.本文将介绍如何读写Andr ...

  2. 解决iScroll中事件点击一次却触发两次的问题

    var t1=null;//全局 function myClick() { if (t1 == null){ t1 = new Date().getTime(); }else{ var t2 = ne ...

  3. 转--DataTable 修改列名 删除列 调整列顺序

    DataTable myDt =dt; //删除列 myDt.Columns.Remove("minArea"); myDt.Columns.Remove("maxAre ...

  4. svn出错问题(用户名密码有修改以及资源url改变时)

    用eclipse 同步SVN服务器宛然无法访问了: org.tigris.subversion.javahl.ClientException: RA layer request failed svn: ...

  5. Java-struts2 之中文乱码问题

    中文乱码问题,是个很麻烦的问题,有时候你发现,你表单页面的编码是UTF-8 Stutrst.xml也有这么一句话 <constant name="struts.i18n.encodin ...

  6. SQLSERVER2008 显示列信息,包含扩展属性

    select b.name as table_name,a.name as column_name,t.name type_name        ,a.max_length ,a.precision ...

  7. html-----002

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. mysql主要应用场景 转载

    前言 据说目前MySQL用户已经达千万级别了,其中不乏企业级用户.可以说是目前最为流行的开源数据库管理系统软件了.任何产品都不可能是万能的,也不可能适用于所有的应用场景.那么MySQL到底在什么场景下 ...

  9. cygwin 扩展

    1.使用setup,然后一路安装到select package,选择需要的包即可,然后一路next. 2.setup.exe -q -P 包名, 详细用法如下: Command Line Option ...

  10. 网站开发常用jQuery插件总结(二)弹出层插件Lightbox_me

    网站开发过程中,为了增加网站交互效果,我们有时需要在当前页面弹出诸如登陆.注册.设置等窗口.而这些窗口就是层,弹出的窗口就是弹出层.jQuery中弹出层插件很多,但有些在html5+css3浏览器下, ...