BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP

Description

有一排n棵树,第i棵树的高度是Di。
MHY要从第一棵树到第n棵树去找他的妹子玩。
如果MHY在第i棵树,那么他可以跳到第i+1,i+2,...,i+k棵树。
如果MHY跳到一棵不矮于当前树的树,那么他的劳累值会+1,否则不会。
为了有体力和妹子玩,MHY要最小化劳累值。

Input

There is a single integer N(2<=N<=1 000 000) in the first line of the standard input: the number of trees in the Byteotian Line Forest. The second line of input holds   integers D1,D2…Dn(1<=Di<=10^9) separated by single spaces: Di is the height of the i-th tree.
The third line of the input holds a single integer Q(1<=Q<=25): the number of birds whose flights need to be planned. The following Q lines describe these birds: in the i-th of these lines, there is an integer Ki(1<=Ki<=N-1) specifying the i-th bird's stamina. In other words, the maximum number of trees that the i-th bird can pass before it has to rest is Ki-1.

Output

Your program should print exactly Q lines to the standard output. In the I-th line, it should specify the minimum number of tiresome flight legs of the i-th bird.

Sample Input

9
4 6 3 6 3 7 2 6 5
2
2
5

Sample Output

2
1

首先有DP方程F[i]=min(F[j]+(d[i]>=d[j])) (1<=i-j<=k)

可以发现作为决策点,F值小的更优(因为每次最多只加1),F值相同的高的更优。

于是可以用单调队列优化DP过程。

比较决策点谁更优时先比较F,再比较高度。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 1000500
int f[N],d[N],n,m,Q[N],l,r;
bool check(int j,int k) {
if(f[j]==f[k]) return d[k]>d[j];
return f[k]<f[j];
}
int main() {
scanf("%d",&n);
int i,k;
for(i=1;i<=n;i++) {
scanf("%d",&d[i]);
}
scanf("%d",&m);
while(m--) {
scanf("%d",&k);
l=r=0;
f[1]=0;Q[r++]=1;
for(i=2;i<=n;i++) {
while(l<r&&i-Q[l]>k) l++;
f[i]=f[Q[l]]+(d[i]>=d[Q[l]]);
while(l<r&&check(Q[r-1],i)) r--;
Q[r++]=i;
}
printf("%d\n",f[n]);
}
}

BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP的更多相关文章

  1. 【bzoj3831】[Poi2014]Little Bird 单调队列优化dp

    原文地址:http://www.cnblogs.com/GXZlegend/p/6826475.html 题目描述 In the Byteotian Line Forest there are   t ...

  2. 洛谷 P3580 - [POI2014]ZAL-Freight(单调队列优化 dp)

    洛谷题面传送门 考虑一个平凡的 DP:我们设 \(dp_i\) 表示前 \(i\) 辆车一来一回所需的最小时间. 注意到我们每次肯定会让某一段连续的火车一趟过去又一趟回来,故转移可以枚举上一段结束位置 ...

  3. bzoj3831 [Poi2014]Little Bird 单调队列优化dp

    3831: [Poi2014]Little Bird Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 505  Solved: 322[Submit][ ...

  4. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  5. bzoj1855: [Scoi2010]股票交易--单调队列优化DP

    单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...

  6. hdu3401:单调队列优化dp

    第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...

  7. Parade(单调队列优化dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others)    ...

  8. 【单调队列优化dp】 分组

    [单调队列优化dp] 分组 >>>>题目 [题目] 给定一行n个非负整数,现在你可以选择其中若干个数,但不能有连续k个数被选择.你的任务是使得选出的数字的和最大 [输入格式] ...

  9. [小明打联盟][斜率/单调队列 优化dp][背包]

    链接:https://ac.nowcoder.com/acm/problem/14553来源:牛客网 题目描述 小明很喜欢打游戏,现在已知一个新英雄即将推出,他同样拥有四个技能,其中三个小技能的释放时 ...

随机推荐

  1. js中获取方法名

    var tmp = arguments.callee.toString(); var re = /function\s*(\w*)/i; var matches = re.exec(tmp);//方法 ...

  2. 编程题:利用for循环打印 9*9 表?

    利用for循环打印 9*9  表? 1*1=1 1*2=2  2*2=4 1*3=3  2*3=6  3*3=9 1*4=4  2*4=8  3*4=12  4*4=16 1*5=5  2*5=10  ...

  3. LOVO学习之思维导图和文档编辑器

    思维导图——是一种图示笔记方法,一种图示笔记工具,一个思考的利器.能将放射性思考具体化,帮助人们理解和记忆事物. 思维导图绘制规则:1,在纸的正中央用一个彩色图像或者符号开始画思维导图. 2,把所有主 ...

  4. redis+twemproxy实现redis集群

    Redis+TwemProxy(nutcracker)集群方案部署记录 转自: http://www.cnblogs.com/kevingrace/p/5685401.html Twemproxy 又 ...

  5. VS 和 VAssistX 常用快捷键

    ----------------------------------------------------------------函数跳转-------------------------------- ...

  6. sqlite db数据的导出

    sqlite的db数据一般是filename.db的格式,用普通文本编辑器打开是乱码,用sqlite名令操作比较麻烦,有时版本格式问题还会起阻扰,有一个GUI工具可以对sqlite db格式数据进行管 ...

  7. Neo4j安装后的密码修改

    首先默认用户名/密码是neo4j/neo4j. 在安全验证打开的时候,你访问服务器/db/data之类的地址可能会提示您以下信息: { "password_change" : &q ...

  8. Nodejs http-proxy代理实战应用

    var https = require('https'); var express = require('express'); var app = express() var http = requi ...

  9. mac上php版本切换

    目标:Mac 环境下完成 php 版本之间的切换 在本地开发中很多时候我们需要多个版本的 php 开发环境.在公司中习惯用自己电脑开发的伙伴们,常常因为公司线上环境被迫更换php版本.但有不想降低自己 ...

  10. Yii2表单提交(带文件上传)

    今天写一个php的表单提交接口,除了基本的字符串数据,还带文件上传,不用说前端form标签内应该有这些属性 <form enctype="multipart/form-data&quo ...