【CF】474E Pillars
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的更多相关文章
- 【CF】438E. The Child and Binary Tree
http://codeforces.com/contest/438/problem/E 题意:询问每个点权值在 $c_1, c_2, ..., c_m$ 中,总权值和为 $s$ 的二叉树个数.请给出每 ...
- 【CF】148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题意:w个白b个黑,公主和龙轮流取,公主先取,等概率取到一个.当龙取完后,会等概率跳出一只.(0<= ...
- 【CF】328 D. Super M
这种图论题已经变得简单了... /* D */ #include <iostream> #include <string> #include <map> #incl ...
- 【CF】323 Div2. D. Once Again...
挺有意思的一道题目.考虑长度为n的数组,重复n次,可以得到n*n的最长上升子序列.同理,也可以得到n*n的最长下降子序列.因此,把t分成prefix(上升子序列) + cycle(one intege ...
- 【CF】7 Beta Round D. Palindrome Degree
manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...
- 【CF】86 B. Petr#
误以为是求满足条件的substring总数(解法是KMP分别以Sbeg和Send作为模式串求解满足条件的position,然后O(n^2)或者O(nlgn)求解).后来发现是求set(all vali ...
- 【CF】121 Div.1 C. Fools and Roads
题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...
- 【CF】310 Div.1 C. Case of Chocolate
线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...
- 【CF】110 Div.1 B. Suspects
这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...
随机推荐
- Command Line-Version (SetACL.exe) – Syntax and Description
For a quick start, tell SetACL the following: Object name (-on): This is the path to the object SetA ...
- 总结如何实现Android浮动层,主要是dialog的使用
自定义一个类继承自Dialog类,然后在构造方法中,定义这个dialog的布局和一些初始化信息. 查看源码打印? 01 public class MenuDialog extends Dialog { ...
- Oracle REGEXP_INSTR 用法
原文出处 ORACLE中的支持正则表达式的函数主要有下面四个: 1,REGEXP_LIKE :与LIKE的功能相似 2,REGEXP_INSTR :与INSTR的功能相似 3,REG ...
- 版本控制SVN与Eclipse4.4.1集成 ( eclipse svn:E175002错误解决 )
Eclipse版本--Luna Service Release 1(4.4.1) SVN版本-----1.8.X 系统OS-------ubuntu 14.04 LTS 1.通过地址安装 Help-& ...
- Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合
项目环境背景: 操作系统:win7 JDK:1.7 相关依赖包,截图如下:
- ACCESS表与CSV文件相互导入、导出的SQL语句
一.将ACCESS表导出为CSV文件:Select * INTO [TEXT;FMT=CSV;DELIMITED;HDR=YES;DATABASE=E:\temp\].test.csv FROM Sh ...
- (转)asp.net基础-HttpModule
HttpModule是向实现类提供模块初始化和处置事件.当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于H ...
- Windows下的进程【一】
什么是进程?进程就是一个正在运行的程序的实例,由两部分组成: 内核对象.操作系统用内核对象对进程进行管理,内核对象是操作系统保存进程统计信息的地方. 地址空间.其中包含所有可执行文件或DLL模块的代码 ...
- [Twisted] transport和protocol解耦
Twisted中transport和protocol完全解耦. 这样设计的优点: 1.不同的Protocol协议实现可以重用相同类型的transport. 2.方便测试:假如测试一个协议实现,可以使用 ...
- OC细节 - 1.深拷贝与浅拷贝详解
概述 拷贝:复制一个与源对象内容相同的对象 实现拷贝,需要遵守以下两个协议 NSCopying NSMutableCopying 拷贝返回对象的种类 可变,mutableCopy消息返回的对象 不可变 ...