【CF】474E Pillars
H的范围是10^15,DP方程很容易想到。但是因为H的范围太大了,而n的范围还算可以接受。因此,对高度排序排重后。使用新的索引建立线段树,使用线段树查询当前高度区间内的最大值,以及该最大值的前趋索引。线段树中的结点索引一定满足i<j的条件,因为采用从n向1更新线段树结点。每次线段树查询操作就可以得到argmax(dp[L, R]),很据不等式很容易得到L和R的范围。
1 /* 474E */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <algorithm> 10 #include <cstdio> 11 #include <cmath> 12 #include <ctime> 13 #include <cstring> 14 #include <climits> 15 #include <cctype> 16 using namespace std; 17 18 #define lson l, mid, rt<<1 19 #define rson mid+1, r, rt<<1|1 20 21 typedef struct { 22 __int64 mx; 23 int fa; 24 } node_t; 25 26 const int maxn = 1e5+5; 27 node_t nd[maxn<<2]; 28 __int64 h[maxn], hh[maxn]; 29 int fa[maxn], dp[maxn]; 30 31 void PushUp(int rt) { 32 int lb = rt<<1; 33 int rb = rt<<1|1; 34 35 if (nd[lb].mx >= nd[rb].mx) { 36 nd[rt].fa = nd[lb].fa; 37 nd[rt].mx = nd[lb].mx; 38 } else { 39 nd[rt].fa = nd[rb].fa; 40 nd[rt].mx = nd[rb].mx; 41 } 42 } 43 44 void build(int l, int r, int rt) { 45 nd[rt].mx = -1; 46 nd[rt].fa = 0; 47 if (l == r) 48 return ; 49 int mid = (l+r)>>1; 50 build(lson); 51 build(rson); 52 } 53 54 void update(int x, int in, int l, int r, int rt) { 55 if (l == r) { 56 if (nd[rt].mx < dp[in]) { 57 nd[rt].mx = dp[in]; 58 nd[rt].fa = in; 59 } 60 return ; 61 } 62 int mid = (l+r)>>1; 63 if (x <= mid) 64 update(x, in, lson); 65 else 66 update(x, in, rson); 67 PushUp(rt); 68 } 69 70 node_t query(int L, int R, int l, int r, int rt) { 71 node_t d; 72 if (L<=l && R>=r) 73 return nd[rt]; 74 int mid = (l+r)>>1; 75 if (R <= mid) { 76 return query(L, R, lson); 77 } else if (L > mid) { 78 return query(L, R, rson); 79 } else { 80 node_t ln = query(L, R, lson); 81 node_t rn = query(L, R, rson); 82 return rn.mx>ln.mx ? rn:ln; 83 } 84 } 85 86 int main() { 87 int i, j, k; 88 int n, m, d; 89 int ans, v; 90 int l, r; 91 node_t node; 92 __int64 tmp; 93 94 #ifndef ONLINE_JUDGE 95 freopen("data.in", "r", stdin); 96 freopen("data.out", "w", stdout); 97 #endif 98 99 scanf("%d %d", &n, &d); 100 for (i=1; i<=n; ++i) { 101 scanf("%I64d", &h[i]); 102 hh[i] = h[i]; 103 } 104 sort(h+1, h+1+n); 105 m = unique(h+1, h+1+n) - (h+1); 106 build(1, m, 1); 107 108 for (i=n; i>0; --i) { 109 dp[i] = 1; 110 fa[i] = 0; 111 112 tmp = hh[i] + d; 113 if (tmp <= h[m]) { 114 l = lower_bound(h+1, h+1+m, tmp) - h; 115 node = query(l, m, 1, m, 1); 116 if (node.mx+1 > dp[i]) { 117 dp[i] = node.mx + 1; 118 fa[i] = node.fa; 119 } 120 } 121 122 tmp = hh[i] - d; 123 if (tmp >= h[1]) { 124 r = m+1; 125 if (tmp < h[m]) 126 r = upper_bound(h+1, h+1+m, tmp) - h; 127 node = query(1, r-1, 1, m, 1); 128 if (node.mx+1 > dp[i]) { 129 dp[i] = node.mx + 1; 130 fa[i] = node.fa; 131 } 132 } 133 134 l = lower_bound(h+1, h+1+m, hh[i]) - h; 135 update(l, i, 1, m, 1); 136 } 137 138 ans = dp[1]; 139 v = 1; 140 for (i=2; i<=n; ++i) { 141 if (dp[i] > ans) { 142 ans = dp[i]; 143 v = i; 144 } 145 } 146 147 printf("%d\n", ans); 148 printf("%d", v); 149 while (fa[v]) { 150 v = fa[v]; 151 printf(" %d", v); 152 } 153 putchar('\n'); 154 155 #ifndef ONLINE_JUDGE 156 printf("%d\n", (int)clock()); 157 #endif 158 159 return 0; 160 }
转载于:https://www.cnblogs.com/bombe1013/p/4423223.html
【CF】474E Pillars相关推荐
- 【cf】Codeforces 题解等汇总
[cf]Codeforces Round #774 (Div. 2) 前4题 [cf]Codeforces Round #774 (Div. 2) 前4题_legend_yst的博客-CSDN博客 [ ...
- cf端游界面更新显示服务器繁忙,【CF】UI界面更新了,那玩家期待已久的经典服务器呢?...
原标题:[CF]UI界面更新了,那玩家期待已久的经典服务器呢? Hello,大家好,我是你们的灵狐姐,相信小伙伴们都知道新版本更新的所有内容了!目前体验服已经正式上线,相信不久后正服也是马上更新了!本 ...
- 【cf】Codeforces Round #784(Div 4)
由于一次比赛被虐得太惨,,生发开始写blog的想法,于是便有了这篇随笔(找了个近期的cf比赛练练手(bushi))第一次写blog,多多包涵. 第二场cf比赛,第一场打的Div2,被虐太惨,所以第二场 ...
- 【CF】142 Div.1 B. Planes
SPFA.注意状态转移条件,ans的求解需要在bfs中间求解.因为只要到了地点n,则无需等待其他tourist. 还是蛮简单的,注意细节. 1 /* 229B */ 2 #include <io ...
- 【CF】556D A Lot of Games
构建trie树,可以得到4类结点:必胜点,必负点,完全主宰点(可胜可负),完全无法主宰点(无法控制最终胜负).递归到叶子结点,即为必胜点,回溯分情况讨论.注意叶子结点使用属性n来控制,n表示当前结点的 ...
- 【CF】304 E. Soldier and Traveling
基础网络流,增加s和t,同时对于每个结点分裂为流入结点和流出结点.EK求最大流,判断最大流是否等于当前总人数. 1 /* 304E */ 2 #include <iostream> 3 # ...
- 【CF】3B Lorry
这道题目网上有几个题解,均有问题.其实就是简单的贪心+排序,没必要做的那么复杂. 一旦tot+curv > v时,显然curv==2, 有三种可能: (1)取出最小的curv==1的pp,装入当 ...
- 【CF】Codeforces 1702F
Equate Multisets 题目描述 Multiset -is a set of numbers in which there can be equal elements, and the or ...
- 【CF】1773K-King‘s Puzzle 题解
传送门:1773K 标签:图论 题目大意 给定n个点,序号分别为1到n.你需要构造一个连通图,图中要包含所有的点,且满足以下要求: 1.构成的图必须是简单图. 2.图中总边数m满足0<=m &l ...
最新文章
- 清理服务器,不可不知的两个指令
- 单身的我听着单身情歌。。。看着那些儿时看过的小说。。。
- mybatis04--Mapper动态代理实现
- JavaScript学习笔记05【高级——DOM对象】
- 南京工程学院计算机组成原理第二版答案,南大__计算机组成原理第二次作业
- POJ 2353 DP
- leetcode981. 基于时间的键值存储(treemap)
- 手机APP和WAP版的区别
- 大数据学习系列----大数据项目的思考
- 今晚直播丨如何通过APEX实现数据库自动晨检功能
- 先河系统为你讲解私有云服务器的优点
- PCL_PCLVisualizer在多线程中的使用问题(viewer spinOnce crash)
- .so 依赖目录 cmake_Android Studio 生成 so 文件
- 饭卡管理系统mysql_数据库饭卡管理系统.doc
- html 英文发音,一些英文字母的发音
- oracle算加权平均的函数,EXCEL中用公式算加权平均值
- POJ 3026 Borg Maze(BFS+最小生成树)
- vmware安装win10镜像出现boot Manager
- 库房出入货物程序设计
- 尝试自己制作一个网页版的单机冒险岛!!
热门文章
- web.config/app.config敏感数据加/解密的二种方法
- 对于有A[0…N]有序的数组,判断是否存在A[i]=I 如果存在的话返回Index,
- 指尖触碰样书,梦想照进现实「博客出书的故事③」
- ICMP隧道工具ptunnel
- Visual Studio 2017为Android APK包签名
- 编码/解码和进制转化工具hURL
- Unity 2D游戏开发教程之游戏中精灵的跳跃状态
- iOS 9应用开发教程之多行读写文本ios9文本视图
- Android 2D游戏引擎AndEngine配置环境
- centos得mysql安装教程_Centos下Mysql安装图文教程_MySQL