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

然后比赛的时候对每个答案长度为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. HDOJ 1081(ZOJ 1074) To The Max(动态规划)

    Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...

  2. IOS引用的静态库里包含category文件出现“unrecognized selector”的解决办法

    来自链接:http://blog.csdn.net/ccf0703/article/details/8279187 针对静态库工程中的Category,在被其他工程引入的时候,会出现selector ...

  3. JavaScript高级程序设计21.pdf

    第10章 DOM DOM(文档对象模型)是针对HTML和XML文档的一个API(应用程序编程接口) IE中所有DOM对象都是以COM对象的形式实现的,这意味着IE中的对象与原生JavaScript对象 ...

  4. Ural 1043 Cover the Arc

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1043 题目大意:一个2000*2000方格坐标,x,y范围都是[-1000,1000]. ...

  5. TCP连接建立的三次握手过程可以携带数据吗?

    前几天实验室的群里扔出了这样一个问题:TCP连接建立的三次握手过程可以携带数据吗?突然发现自己还真不清楚这个问题,平日里用tcpdump或者Wireshark抓包时,从来没留意过第三次握手的ACK包有 ...

  6. cf493A

    Description Vasya has started watching football games. He has learned that for some fouls the player ...

  7. Ehcache专栏

    http://www.iteye.com/blogs/subjects/ehcache

  8. Codeforces 15B Laser

    题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...

  9. 查询,创建,扩充表空间&&impdp--------表空间大全

    周六晚上还在办公室导入数据. 按schemas导入成功的关键是:导入的环境和源数据环境里面的表空间大小,表空间名字,需要一模一样 (当然,表空间大小创建到和源数据环境里面ues_size大小就可以了) ...

  10. web.xml常用标签整理(不定期更新)

    <?xml version="1.0" encoding="UTF-8"?><!-- 标明使用的XML版本和文档编码,此项必须位于第一行,之前 ...