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

然后比赛的时候对每个答案长度为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. jQuery zxxbox弹出框插件(v3.0)

    插件地址: http://www.zhangxinxu.com/study/201009/jquery-zxxbox-v3-demo.html

  2. 万能的Volley

    v1olley能干那些事?发送get请求 public void getJson() { String url = "http://"+host+":8080/web/j ...

  3. Linux学习笔记1——Linux的目录结构

    / 是根目录 ~是主目录 bin 存放二进制可执行文件(Is,cat,mkdir等) boot 存放用于系统引导时使用的各种文件 dev 用于存放设备文件 etc 存放系统配置文件 home 存放所有 ...

  4. HDOJ1518Square 深搜

    Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  5. Supervisord管理

    原文地址:http://blog.csdn.net/fyh2003/article/details/6837970 学习笔记 Supervisord可以通过sudo easy_install supe ...

  6. tabhost中activity跳转动画不显示的解决办法

    [1]如果是tabhost中的activity跳到其他的activity,用这篇blog的方法即可 http://blog.sina.com.cn/s/blog_8db8914301010t31.ht ...

  7. 324. Wiggle Sort II

    这个题真是做得我想打人了.我生起气来连自己都打. 一开始quick select没啥可说的.但是in place把老命拼了都没想出来.. 看网上的答案是3 partition,MAP式子一看就由衷地反 ...

  8. jquery 弹出层

    <!DOCTYPE html> <html>     <head>         <meta charset="utf-8">   ...

  9. hdu 2457 DNA repair

    AC自动机+DP.按着自动机跑,(其实是生成新的满足题目要求的串,然后找改变最少的.)但是不能跑到是单词的地方,如果跑到单词的话那么说明改变后的串含有病毒了,不满足题意.然后就是应该怎么跑的问题了,现 ...

  10. Java基础知识强化之集合框架笔记16:List集合的特有功能概述和测试

    1. List集合的特有功能概述: (1)添加功能: void add(int index, Object element):在指定位置添加元素 (2)获取功能: Object get(int ind ...