题干:

Description

In the Byteotian Line Forest there are   trees in a row. On top of the first one, there is a little bird who would like to fly over to the top of the last tree. Being in fact very little, the bird might lack the strength to fly there without any stop. If the bird is sitting on top of the tree no.  , then in a single flight leg it can fly to any of the trees no.i+1,i+2…I+K, and then has to rest afterward.

Moreover, flying up is far harder to flying down. A flight leg is tiresome if it ends in a tree at least as high as the one where is started. Otherwise the flight leg is not tiresome.

The goal is to select the trees on which the little bird will land so that the overall flight is least tiresome, i.e., it has the minimum number of tiresome legs. We note that birds are social creatures, and our bird has a few bird-friends who would also like to get from the first tree to the last one. The stamina of all the birds varies, so the bird's friends may have different values of the parameter  . Help all the birds, little and big!

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

Hint

Explanation: The first bird may stop at the trees no. 1, 3, 5, 7, 8, 9. Its tiresome flight legs will be the one from the 3-rd tree to the 5-th one and from the 7-th to the 8-th.

题目大意:

有一排n棵树,第i棵树的高度是Di。

MHY要从第一棵树到第n棵树去找他的妹子玩。

如果MHY在第i棵树,那么他可以跳到第i+1,i+2,...,i+k棵树。

如果MHY跳到一棵不矮于当前树的树,那么他的劳累值会+1,否则不会。

为了有体力和妹子玩,MHY要最小化劳累值。

解题报告:

dp[i]表示从第一棵树到第i棵树所需的最小疲劳值。dp[i] = min(dp[j] + (s[i] > s[j]))

单调队列中的元素主要考虑它的时效性和价值,时效性用来删除队头,价值和时效性综合考虑删除队尾。

单调队列中的时效性是越靠后(在队列中)越好,那么队列中元素的价值是:疲劳值和树高的综合考虑。

注意,如果对于两个位置j1和j2,有f[j1]<f[j2],则j1一定比j2更优。因为就算j1高度比较矮,到达i顶多再多消耗1个疲劳值,顶多和j2相等。如果不需要消耗疲劳值,比j2更优。 如果f[j1]=f[j2],则我们比较它们的高度D,高度高的更优。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1e6 + 5;
const ll INF = 0x3f3f3f3f;
ll a[MAX],dp[MAX];
int n,q,k;
deque<ll> dq;
int main()
{cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i);cin>>q;while(q--) {scanf("%d",&k);while(dq.size()) dq.pop_back();dq.push_back(1);dp[1] = 0;for(int i = 2; i<=n; i++) {dp[i] = INF;while(!dq.empty() && dq.front() < i-k) dq.pop_front();dp[i] = dp[dq.front()] + (a[i] >= a[dq.front()]);//注意这两句的顺序!! while(!dq.empty() && (dp[dq.back()] > dp[i] || (dp[dq.back()]==dp[i]&&a[dq.back()]<=a[i])))//加等号是为了节省时间吧?因为这样dq里的元素就少了。 dq.pop_back();dq.push_back(i);}printf("%lld\n",dp[n]);}return 0 ;
}

【BZOJ 3831】【Poi2014】Little Bird(单调队列优化dp)相关推荐

  1. 『单调队列优化DP』[POI2014]ZAL-Freight

    Problem\mathrm{Problem}Problem Upper Bytown和Lower Bytown的火车站通过一条轨道铁路连接. 沿任何一个方向在它们之间行驶都需要s分钟. 但是,离开车 ...

  2. 算法笔记--单调队列优化dp

    单调队列:队列中元素单调递增或递减,可以用双端队列实现(deque),队列的前面和后面都可以入队出队. 单调队列优化dp: 问题引入: dp[i] = min( a[j] ) ,i-m < j ...

  3. poj 2373(单调队列优化dp)

    在长为L(<=1000000)的草地(可看成线段)上装喷水头,喷射是以这个喷水头为中心,喷水头的喷洒半径是可调节的调节范围为[a,b].要求草地的每个点被且只被一个喷水头覆盖,并且有些连续区间必 ...

  4. poj 1821(单调队列优化dp)

    题意:有一道线性篱笆由N个连续的木板组成.有K个工人,你要叫他们给木板涂色.每个工人有3个参数:L 表示 这个工人可以涂的最大木板数目,S表示这个工人站在哪一块木板,P表示这个工人每涂一个木板可以得到 ...

  5. 洛谷P3195 [HNOI2008]玩具装箱TOY(单调队列优化DP)

    题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具, ...

  6. 【计蒜客 - 蓝桥训练】蒜厂年会(单调队列优化dp,循环数列的最大子段和)

    题干: 在蒜厂年会上有一个抽奖,在一个环形的桌子上,有 nn 个纸团,每个纸团上写一个数字,表示你可以获得多少蒜币.但是这个游戏比较坑,里面竟然有负数,表示你要支付多少蒜币.因为这些数字都是可见的,所 ...

  7. 单调队列以及单调队列优化DP

    单调队列定义: 其实单调队列就是一种队列内的元素有单调性的队列,因为其单调性所以经常会被用来维护区间最值或者降低DP的维数已达到降维来减少空间及时间的目的. 单调队列的一般应用: 1.维护区间最值 2 ...

  8. POJ 1821 Fence(单调队列优化DP)

    题解 以前做过很多单调队列优化DP的题. 这个题有一点不同是对于有的状态可以转移,有的状态不能转移. 然后一堆边界和注意点.导致写起来就很难受. 然后状态也比较难定义. dp[i][j]代表前i个人涂 ...

  9. 【单调队列优化DP】烽火传递 LibreOJ - 10180

    题目来源 点我进入提交题目 反思 因为目前在学习单调队列优化DP,所以会往单调队列上面想.然后犯了一个错误就是,认为这个题目只要用单调队列就可以完成,单调队列只是用来减少时间复杂度的,遇到了求最优解的 ...

  10. AcWing 1089 烽火传递 题解(动态规划—DP—单调队列优化DP)

    AcWing 1089 烽火传递 单调队列优化DP,思路比较简单,维护一个保持元素单调递增的单调队列,队首就是第i座烽火台能接收到的,代价最小的方案,加上第i座烽火台的代价就是这座烽火台的最小值 #i ...

最新文章

  1. p6安装后怎么连接oracle的数据库,p6-oraclexe数据库连接不上处理方法内部版20151012.doc...
  2. 本地方法接口和本地方法栈
  3. import android.view.window;,尝试在空对象引用上调用虚拟方法‘android.view.Window$回调...
  4. sqlserver存储过程的参数传递注意事项
  5. 事件流调试器查看Retract事件
  6. 2016陕西省ACM省赛 HE 字符串处理 删除注释
  7. 300. 最长上升子序列
  8. 如何保证redis高可用?薪资翻倍
  9. TypeError: object of type 'zip' has no len()、'zip' object is not subscriptable
  10. 现代程序设计 作业 第1次
  11. 案例29-购物车提交订单
  12. apache服务通常启动,但打不开网页,提示Try using the Win32DisableAcceptEx directive (转)...
  13. 基于Matpower的电力系统潮流计算原理及仿真设计(详细)
  14. C语言 fprintf 函数 - C语言零基础入门教程
  15. Linux 错误E45,readonly optionisset(add ...)
  16. C++大写字母转小写字母
  17. 北京跑步入夏--妞妞跑步长大
  18. Python爬取URP教务系统课程表并保存到excel
  19. oracle10gwin,win10系统没有法安装Oracle10g如何办?
  20. spring.log

热门文章

  1. 【IE大叔的嘴歪眼斜】之—— 由hasLayout引发的临床CSS Bug表
  2. String ... String 三个点 jdk1.5的特性.才知道
  3. Depth-first Search深度优先搜索专题6
  4. LinkedList专题1
  5. [Leedcode][JAVA][第470题][Ran7()实现Rand10()]
  6. 【软考】[信息安全工程师]
  7. 2021年河南高考成绩排名查询一分一段表,2018河南高考一分一段统计表,查排名必备!...
  8. controller调用controller的方法_SpringCloud(5):Feign整合Ribbon和Hystrix来进行远程调用与服务熔断...
  9. python中什么是数据驱动_Python数据驱动DDT的应用
  10. linux 自带 mysql,linux下安装mysql