单调队列应用--BZOJ 3831 Little Bird

3831: [Poi2014]Little Bird

Time Limit: 20 Sec  Memory Limit: 128 MB

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!

有一排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

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.

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

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

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

 1 #include<cstring>
 2 #define N 1000050
 3 #include<iostream>
 4 using namespace std;
 5 #include<cstdio>
 6 int Q[N],head=0,tail=1,n,m,k,hig[N];
 7 int f[N];
 8 void input()
 9 {
10     scanf("%d",&n);
11     for(int i=1;i<=n;++i)
12       scanf("%d",&hig[i]);
13 }
14 bool cmp(int i,int j)
15 {
16     if(f[i]!=f[j]) return f[i]<f[j];
17     return hig[i]>=hig[j];
18 }
19 int main()
20 {
21     input();
22     scanf("%d",&m);
23     for(int i=1;i<=m;++i)
24     {
25         scanf("%d",&k);
26         memset(Q,0,sizeof(Q));
27         memset(f,0,sizeof(f));
28         f[1]=0;head=0;tail=1;
29         Q[0]=1;
30         for(int j=2;j<=n;++j)
31         {
32             while(head<tail&&j-Q[head]>k)
33               head++;
34             f[j]=f[Q[head]]+(hig[j]>=hig[Q[head]]);
35             while(head<tail&&cmp(j,Q[tail-1]))
36               tail--;
37             Q[tail++]=j;
38         }
39         printf("%d\n",f[n]);
40     }
41     return 0;
42 }
时间: 2024-10-10 07:01:55

单调队列应用--BZOJ 3831 Little Bird的相关文章

BZOJ 3831 POI 2014 Little Bird 单调队列DP

题目大意:给出一片树林,树排成一排,每一棵树都有一个高度.从地一棵树出发,每次可以跳到i+k棵之前,跳到小于自己高度的树上不需要花费体力,反之需要花费一点体力,问到最后一棵树最少需要多少体力. 思路:简单DP方程:f[i] = min{f[j] + (height[i] >= height[j])} 然后发现数据范围只有O(n)可以过. 维护单调队列,队列中按照f单调递减,队尾按照时间往出弹. 当f值相同的时候,高度较高的优先. CODE: #include <queue> #inclu

bzoj 3831: [Poi2014]Little Bird

3831: [Poi2014]Little Bird 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

BZOJ 3831: [Poi2014]Little Bird【动态规划】

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 with

BZOJ 1855 股票交易(单调队列优化DP)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1855 题意:最近lxhgww又迷上了投资股票, 通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股票的走势,第i天的股票买入价为每股APi,第i天的股票卖出价为每股BPi(数据保证对于每 个i,都有APi>=BPi),但是每天不能无限制地交易,于是股票交易所规定第i天的一次买入至多只能购买ASi股,一次卖出至多只能卖出BS

【bzoj 1414】对称的正方形 单调队列+manacher

Description Orez很喜欢搜集一些神秘的数据,并经常把它们排成一个矩阵进行研究.最近,Orez又得到了一些数据,并已经把它们排成了一个n行m列的矩阵.通过观察,Orez发现这些数据蕴涵了一个奇特的数,就是矩阵中上下对称且左右对称的正方形子矩阵的个数. Orez自然很想知道这个数是多少,可是矩阵太大,无法去数.只能请你编个程序来计算出这个数. Input 文件的第一行为两个整数n和m.接下来n行每行包含m个正整数,表示Orez得到的矩阵. Output 文件中仅包含一个整数answer

【BZOJ3831】[Poi2014]Little Bird 单调队列

[BZOJ3831][Poi2014]Little Bird 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

[luogu]P3572 [POI2014]PTA-Little Bird(单调队列)

P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn 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 s

单调队列 BZOJ 2096 [Poi2010]Pilots

2096: [Poi2010]Pilots Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 819  Solved: 418[Submit][Status][Discuss] Description Tz又耍畸形了!!他要当飞行员,他拿到了一个飞行员测试难度序列,他设定了一个难度差的最大值,在序列中他想找到一个最长的子串,任意两个难度差不会超过他设定的最大值.耍畸形一个人是不行的,于是他找到了你. Input 输入:第一行两个有空格隔开的整数k(0

BZOJ 1342 Baltic2007 Sound静音问题 单调队列

题目大意:给定一个长度为n的序列,求哪些长度为m的区间满足区间内最大值与最小值之差小于等于c 利用单调队列维护区间内的最大值和最小值- - 硬搞就可以了- - 刷刷水题真爽- - #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 1001001 using namespace std; int n,m,c,a[M]; int q_ma