Codeforces 474E - Pillars
一眼看上去非常像最长不下降子序列。
然后比赛的时候对每个答案长度为k的序列,维护最后一个数的最大值和最小值。
当时不知道为什么认为从长度最长倒推至前面不会太长,于是心满意足地敲了个O(n^2)。结果T了。。。
正确的做法应该用线段树维护,搜起来就是log(n),总的就是O(N*logN);
用非递归的方法写的
只用了77ms
#include <iostream>
#include <cstdio>
using namespace std; #define ll long long const int INF = ; struct node {
ll key;
int pos;
} dtmin[INF], dtmax[INF];
ll n, d, t, x, M;
ll pre[INF];
int f[INF], ans[INF], namax[INF], namin[INF]; inline int Searchmax (int x, ll k) {
while (dtmax[x].key - k >= d) {
if (x > M) return dtmax[x].pos;
if (dtmax[x << | ].key - k >= d) x = x << | ;
else
x = x << ;
}
return ;
}
inline int Searchmin (int x, ll k) {
while (k - dtmin[x].key >= d) {
if (x > M) return dtmin[x].pos;
if (k - dtmin[x << | ].key >= d) x = x << | ;
else
x = x << ;
}
return ;
} inline void modify (int x, ll k, int i) {
if (dtmax[x + M].key < k) namax[x] = i, dtmax[x + M].key = k;
for (int t = x + M; dtmax[t >> ].key < k && t > ; t >>= )
dtmax[t >> ] = dtmax[t]; if (dtmin[x + M].key > k) namin[x] = i, dtmin[x + M].key = k;
for (int t = x + M; dtmin[t >> ].key > k && t > ; t >>= )
dtmin[t >> ] = dtmin[t];
} int main() {
ios::sync_with_stdio (false);
cin >> n >> d;
cin>>x;
M = n;
int tem = ;
for (int k = M; k; tem++, k >>= ) ;
M = << tem;
for (int i = ; i < (M << ) + ; i++) {
dtmax[i].key = , dtmin[i].key = (1LL << );
dtmax[i].pos = i - M, dtmin[i].pos = i - M;
}
dtmax[].key = dtmin[].key = x;
t = f[] = ;
modify (t, x, );
for (int i = ,k; i <= n; i++) {
cin >> x;
if (k = Searchmax (, x) )
if (f[i] < k + )
f[i] = k + , pre[i] = namax[k]; if ((k = Searchmin (, x)))
if (f[i] < k + )
f[i] = k + , pre[i] = namin[k]; if (f[i] > t) t = f[i];
if (f[i] == ) f[i] = ;
modify (f[i], x, i);
}
cout << t << endl;
int p = -;
for (int i = ; i <= n; i++) if (f[i] == t) { p = i; break; }
for (int i = t; i; i--)
ans[i] = p, p = pre[p];
for (int i = ; i <= t; i++)
cout << ans[i] << ' ';
}
Codeforces 474E - Pillars的更多相关文章
- [CF 474E] Pillars (线段树+dp)
题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...
- 【CF】474E Pillars
H的范围是10^15,DP方程很容易想到.但是因为H的范围太大了,而n的范围还算可以接受.因此,对高度排序排重后.使用新的索引建立线段树,使用线段树查询当前高度区间内的最大值,以及该最大值的前趋索引. ...
- 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)
原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解 By 岩之痕 目录: 一:综述 ...
- Codeforces 474 E. Pillars
水太...... E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #474-E(树形dp)
一.题目链接 http://codeforces.com/contest/960/problem/B 二.题意 给定一棵$N$个节点的树,每个节点的权值$V$.定义树中两点$u_1$和$u_m$的权值 ...
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
- Codeforces Beta Round #1 A,B,C
A. Theatre Square time limit per test:1 second memory limit per test:256 megabytes input:standard in ...
随机推荐
- (转载)MySQL默认INFORMATION_SCHEMA,MySQL,TEST三个数据库用途
(转载)http://www.45it.com/database/201204/29390.htm 本文简要说明了MySQL数据库安装好后自带的INFORMATION_SCHEMA,MySQL,TES ...
- C#编程技术层次
不谈具体领域(比如搜索,视频,网络等),单就编程语言这个垂直方向,大体上对它有一个如下的层次划分. 1. 基本运用C#语法,在各种工具和示例代码的支持下,完成一些基本程序任务 2. 熟练掌握面向对象与 ...
- Unity Fresnel Hero(Dota2) Shader
Shader "HeroShader" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _Rim ...
- n个灯,k个人的开灯问题
/**有n个灯,编号为1-n.第一个人把所以灯打开,第二个人按下 所有编号为2的倍数的开关,第三个人按下3的倍数的开关,依次类推, 一共有k个人,问最后有哪些灯开着? 样例输入: 7 3 样例输出: ...
- bzoj 3156 防御准备(斜率DP)
3156: 防御准备 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 837 Solved: 395[Submit][Status][Discuss] ...
- Sicily1151:魔板搜索及优化
最终优化代码地址: https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1151.c 题目如下 Constraints ...
- 找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数
找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数 #include<iostream>using namespace s ...
- Java Struts2 的请求处理流程详解
一.Struts2的处理流程: 客户端产生一个HttpServletRequest的请求,该请求被提交到一系列的标准过滤器(Filter)组建链中(如ActionContextCleanUp:它主要是 ...
- Centos 下安装MongoDB
Centos 下安装MongoDB 一.安装方法 方法(一) 1 配置包管理系统 创建/etc/yum.repos.d/mongodb.repo 文件,当然我们使用的是64位系统,32位的情况不再考 ...
- Mysql性能优化那些事
对于全栈而言,数据库技能不可或缺,关系型数据库或者nosql,内存型数据库或者偏磁盘存储的数据库,对象存储的数据库或者图数据库--林林总总,但是第一必备技能还应该是MySQL.从LAMP的 ...