题目链接:http://codeforces.com/problemset/problem/1029/B

题目大意:从数组a中选出一些数组成数组b,要求 b[i+1]<=b[i]*2

一开始想到的是O(n^2)的动态规划,但是超时了,下面是超时的代码。

#include <iostream>
using namespace std; const int maxn = 200020; int n, a[maxn], f[maxn], res = 0; int main() {
cin >> n;
for (int i = 0; i < n; i ++) cin >> a[i];
for (int i = 0; i < n; i ++) {
f[i] = 1;
for (int j = i-1; j >= 0 && a[i] <= 2*a[j]; j --) {
f[i] = max(f[i], f[j]+1);
}
if (f[i] > res) res = f[i];
}
cout << res << endl;
return 0;
}

然后想到的是:

  • 二分找最小的满足a[i]<=a[j]*2的那个j ,O(logn)的时间复杂度
  • 线段树求区间[j,i-1]的最大值,O(logn)的时间复杂度

再加上外层的循环,时间复杂度会降到O(n * logn)。

代码:

#include <iostream>
using namespace std; #define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1 const int maxn = 200020; int n, a[maxn], MAX[maxn<<2]; void pushUp(int rt) {
MAX[rt] = max(MAX[rt<<1] , MAX[rt<<1|1]);
} void build(int l, int r, int rt) {
if (l == r) {
MAX[rt] = 0;
return;
}
int m = (l+r) >> 1;
build(lson);
build(rson);
pushUp(rt);
} void update(int p, int val, int l, int r, int rt) {
if (l == r) {
MAX[rt] = val;
return;
}
int m = (l + r) >> 1;
if (p <= m) update(p, val, lson);
else update(p, val, rson);
pushUp(rt);
} int query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) {
return MAX[rt];
}
int m = (l + r) >> 1;
int tmp = 0;
if (L <= m) tmp = query(L, R, lson);
if (R >= m+1) tmp = max(tmp, query(L, R, rson));
return tmp;
} int findL(int i) {
return lower_bound(a, a+n+1, (a[i]+1)/2) - a;
} int main() {
cin >> n;
build(1, n, 1);
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++) {
int L = findL(i);
int val;
if (L >= i) val = 1;
else {
val = query(L, i-1, 1, n, 1) + 1;
}
update(i, val, 1, n, 1);
}
cout << query(1, n, 1, n, 1) << endl;
return 0;
}

然后是O(n)的单调队列解法。

代码:

#include <iostream>
using namespace std; const int maxn = 200020; int n, a[maxn], MAX[maxn];
int st = 0, ed = 0, sum = 0; int que[maxn]; // que存放id,id对应的最大长度是MAX[id] void test() {
cout << "[test]" << endl;
for (int i = 1; i <= n; i ++) {
cout << i << ": " << MAX[i] << endl;
}
} int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
int j = 1;
for (int i = 1; i <= n; i ++) {
while (j < i && !( a[i] <= 2 * a[j] )) {
j ++;
}
while (st < ed && que[st] < j) st ++;
if (st == ed) {
MAX[i] = 1;
} else {
MAX[i] = MAX[ que[st] ] + 1;
}
sum = max(sum , MAX[i]);
while (st < ed && MAX[ que[st] ] <= MAX[i]) {
st ++;
}
que[ed++] = i;
}
// test();
cout << sum << endl;
return 0;
}

Codeforces 1029B. Creating the Contest 动态规划O(nlogn)解法 及 单调队列O(n)解法的更多相关文章

  1. codeforce1029B B. Creating the Contest(简单dp,简单版单调栈)

    B. Creating the Contest time limit per test 1 second memory limit per test 256 megabytes input stand ...

  2. Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列

    B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...

  3. CodeForces B. Creating the Contest

    http://codeforces.com/contest/1029/problem/B You are given a problemset consisting of nn problems. T ...

  4. 不失一般性和快捷性地判定决策单调(洛谷P1912 [NOI2009]诗人小G)(动态规划,决策单调性,单调队列)

    洛谷题目传送门 闲话 看完洛谷larryzhong巨佬的题解,蒟蒻一脸懵逼 如果哪年NOI(放心我这样的蒟蒻是去不了的)又来个决策单调性优化DP,那蒟蒻是不是会看都看不出来直接爆\(0\)?! 还是要 ...

  5. 洛谷P3628 [APIO2010]特别行动队(动态规划,斜率优化,单调队列)

    洛谷题目传送门 安利蒟蒻斜率优化总结 由于人是每次都是连续一段一段地选,所以考虑直接对\(x\)记前缀和,设现在的\(x_i=\)原来的\(\sum\limits_{j=1}^ix_i\). 设\(f ...

  6. 洛谷P3515 [POI2011]Lightning Conductor(动态规划,决策单调性,单调队列)

    洛谷题目传送门 疯狂%%%几个月前就秒了此题的Tyher巨佬 借着这题总结一下决策单调性优化DP吧.蒟蒻觉得用数形结合的思想能够轻松地理解它. 首先,题目要我们求所有的\(p_i\),那么把式子变一下 ...

  7. Codeforces Round #189 (Div. 2) D. Psychos in a Line 单调队列dp

    D. Psychos in a Line time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #278 (Div. 1) B - Strip dp+st表+单调队列

    B - Strip 思路:简单dp,用st表+单调队列维护一下. #include<bits/stdc++.h> #define LL long long #define fi first ...

  9. 动态规划专题(四)——单调队列优化DP

    前言 单调队列优化\(DP\)应该还算是比较简单容易理解的吧,像它的升级版斜率优化\(DP\)就显得复杂了许多. 基本式子 单调队列优化\(DP\)的一般式子其实也非常简单: \[f_i=max_{j ...

随机推荐

  1. C语言for 循环 9*9 实现九九乘法表

    #include <stdio.h> int main(void) { //for循环实现9*9乘法表 /* 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 */ ...

  2. C++问题--Reis连接redisContext *pRedisContext = redisConnectWithTimeout("127.0.0.1", 6379, tv);pRedisContext->errstr返回错误磁盘空间不足

    一.问题 使用C++连接Redis的时候出错,错误String为磁盘空间不足,连接代码如下: //reids默认监听端口6387 ; struct timeval tv; tv.tv_sec = iT ...

  3. lixuxmint系统定制与配置(2)-输入法

    小书匠Linux RIME的官网在这里 1.安装 刚开始是使用ibus-rime,后来使用过程感觉不舒服,就换回fcitx-rime.使用以下命令安装fcitx-rime sudo apt insta ...

  4. 43、内置函数及每日uv、销售额统计案例

    一.spark1.5内置函数 在Spark 1.5.x版本,增加了一系列内置函数到DataFrame API中,并且实现了code-generation的优化.与普通的函数不同,DataFrame的函 ...

  5. AtCoder Grand Contest 006 题解

    传送门 \(A\) 咕咕 const int N=105; char s[N],t[N];int n; inline bool eq(R int k){fp(i,1,k)if(s[n-k+i]!=t[ ...

  6. UOJ#316. 【NOI2017】泳池 动态规划,Berlekamp-Massey,Cayley-Hamilton定理

    原文链接www.cnblogs.com/zhouzhendong/p/UOJ316.html 题解 首先,我们将答案转化成最大矩形大小 \(\leq k\) 的概率 减去 \(\leq k-1\) 的 ...

  7. avalon如何用年月日的方式输出..

    在avolon里面的http://avalonjs.coding.me/filter.html  可以找到与date相关的转化,如果是要转化为年月日的形式,看下面的代码: <span style ...

  8. GO语言网络编程

    socket编程 Socket是BSD UNIX的进程通信机制,通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.Socket可以理解为TCP/IP网络的API,它 ...

  9. ubuntu之路——day10.5 可避免偏差

    可避免偏差: 总结一下就是当贝叶斯最优误差接近于训练误差的时候,比如下面的例子B,我们不会说我们的训练误差是8%,我们会说我可避免偏差是0.5%.

  10. JSON HiJacking攻击

    JSON劫持类似于CSRF攻击,为了了解这种攻击方式,我们先看一下Web开发中一种常用的跨域获取数据的方式:JSONP. 先说一下JSON吧,JSON是一种数据格式,主要由字典(键值对)和列表两种存在 ...