【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 ...
随机推荐
- 在ASP.NET中ShowModalDialog+ztree的使用
.aspx: <script type="text/javascript"> function getReturnValue() { var strResult = w ...
- c#不重复的排序方法
public int getRandom(int num) { Thread.Sleep(5); // Random ro = new Random(unchecked((int)DateTime.N ...
- C#/.net七牛云存储上传图片(文件)操作
七牛云存储官方: C#SDK(http://developer.qiniu.com/docs/v6/sdk/csharp-sdk.html) 注册成为标准用户就可获得:10GB永久免费存储空间/ 每月 ...
- BI任务列表
了解点击流系统和pv/uv的相关计算 关于pv的那些事!! ···············································2014-09-10 homework做了些什 ...
- CenOs安装中文输入法
http://jingyan.baidu.com/album/d8072ac4434666ec95cefda1.html?picindex=2 查看链接
- Asp.net Mvc对比Php的4大误解
一:asp.net技术已过时,Php技术更新 Asp.net mvc 5 发布于2014 夏天. 二:php开发者更多,所以更能得到帮助 2者对比犹如下图,会拿电锯的肯定多少会点锯子, 会用锯子的不一 ...
- android-satellite-menu
使用过Path的人都应该知道,在Path主界面的左下方有一个非常有意思的菜单.菜单由一个主按钮组成,当用户点击该按钮时,就会有一连串的按钮弹出,而Satellite Menu正是该菜单的一个开源版本. ...
- CSS中:nth-child和JQuery:eq的区别
$("li:nth-child(n)")选择器与$("li:eq(n)")选择器的不同之处在于:$("li:eq(n)")选择器只匹配一个元 ...
- ASP.NET中扩展FileUpload的上传文件的容量
ASP.NET中扩展FileUpload只能上传小的文件,大小在4MB以内的.如果是上传大一点的图片类的可以在web.config里面扩展一下大小,代码如下 <system.web> &l ...
- js输出单一字符字串
<!DOCTYPE HTML> <html> <body> <input type="text" id="str" & ...