一眼看上去非常像最长不下降子序列。

然后比赛的时候对每个答案长度为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的更多相关文章

  1. [CF 474E] Pillars (线段树+dp)

    题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...

  2. 【CF】474E Pillars

    H的范围是10^15,DP方程很容易想到.但是因为H的范围太大了,而n的范围还算可以接受.因此,对高度排序排重后.使用新的索引建立线段树,使用线段树查询当前高度区间内的最大值,以及该最大值的前趋索引. ...

  3. 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)

    原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解    By 岩之痕 目录: 一:综述 ...

  4. Codeforces 474 E. Pillars

    水太...... E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  6. 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 ...

  7. Codeforces Round #474-E(树形dp)

    一.题目链接 http://codeforces.com/contest/960/problem/B 二.题意 给定一棵$N$个节点的树,每个节点的权值$V$.定义树中两点$u_1$和$u_m$的权值 ...

  8. Codeforces Round #271 (Div. 2)题解【ABCDEF】

    Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...

  9. 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 ...

随机推荐

  1. Linux Eclipse代码提示功能设置(Java & C/C++)

    最近在Linux下开发,由于长期使用Visual Studio 2010,对代码提示功能情有独钟,现在在Linux下,使用Eclipse做开发,当然免不了怀念Visual Studio强悍的代码提示, ...

  2. 串口传输文件 lrzsz

    假设有一种开发环境,一块板子,除了串口,没有任何外部出入输出设备,没有sd卡,没有网线,这个时候如果你想跟这块板子传输交互文件,要怎么办? 根据modem所采用的文件传输协议:xmodem,ymode ...

  3. Eclipse下安装及配置maven项目管理工具

    ①eclipse下maven插件安装. 本地maven安装.环境变量配置完成后,打开eclipse,点击eclipse菜单栏Help->Eclipse Marketplace搜索关键字maven ...

  4. 常见sql的error解决方法

    1.sqlserver 2008 login failed: error 18456 这是很可能是因为你没有选择sql and windows登录的模式导致sql server的用户登录失败 解决方法 ...

  5. 介绍两个Eclipse插件: Implementors & Call Hierarchy

    介绍两个Eclipse插件: Implementors & Call Hierarchy 本文介绍两个在Eclipse调试与跟踪过程中的两个实用插件 他们都可以在 http://eclipse ...

  6. nyoj 102 次方求模【快速幂】

    次方求模 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 求a的b次方对c取余的值   输入 第一行输入一个整数n表示测试数据的组数(n<100)每组测试只有一 ...

  7. Java8中Lambda表达式的10个例子

    Java8中Lambda表达式的10个例子 例1 用Lambda表达式实现Runnable接口 //Before Java 8: new Thread(new Runnable() { @Overri ...

  8. mongodb基本概念解析

    MongoDB 概念解析 不管我们学习什么数据库都应该学习其中的基础概念,在mongodb中基本的概念是文档.集合.数据库,下面我们挨个介绍. 下表将帮助您更容易理解Mongo中的一些概念: SQL术 ...

  9. WebLogic SSRF 漏洞 (简要翻译)

    [Ref]http://blog.gdssecurity.com/labs/2015/3/30/weblogic-ssrf-and-xss-cve-2014-4241-cve-2014-4210-cv ...

  10. Qt 学习之路:文件

    文件操作是应用程序必不可少的部分.Qt 作为一个通用开发库,提供了跨平台的文件操作能力.从本章开始,我们来了解下 Qt 的文件以及输入输出的功能,也就是 I/O 系统. Qt 通过QIODevice提 ...