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,高度高的更优。

 #include<cstring>
#define N 1000050
#include<iostream>
using namespace std;
#include<cstdio>
int Q[N],head=,tail=,n,m,k,hig[N];
int f[N];
void input()
{
scanf("%d",&n);
for(int i=;i<=n;++i)
scanf("%d",&hig[i]);
}
bool cmp(int i,int j)
{
if(f[i]!=f[j]) return f[i]<f[j];
return hig[i]>=hig[j];
}
int main()
{
input();
scanf("%d",&m);
for(int i=;i<=m;++i)
{
scanf("%d",&k);
memset(Q,,sizeof(Q));
memset(f,,sizeof(f));
f[]=;head=;tail=;
Q[]=;
for(int j=;j<=n;++j)
{
while(head<tail&&j-Q[head]>k)
head++;
f[j]=f[Q[head]]+(hig[j]>=hig[Q[head]]);
while(head<tail&&cmp(j,Q[tail-]))
tail--;
Q[tail++]=j;
}
printf("%d\n",f[n]);
}
return ;
}

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

  1. 单调队列优化DP || [Poi2014]Little Bird || BZOJ 3831 || Luogu P3572

    题面:[POI2014]PTA-Little Bird 题解: N<=1e6 Q<=25F[i]表示到达第i棵树时需要消耗的最小体力值F[i]=min(F[i],F[j]+(D[j]> ...

  2. bzoj 3831 Little Bird (单调队列优化dp)

    /*先贴个n*n的*/ #include<iostream> #include<cstdio> #include<cstring> #define maxn 100 ...

  3. 【单调队列】bzoj 1407 [HAOI2007]理想的正方形

    [题意] 给定一个n*m的矩阵,求所有大小为k*k的正方形中(最大值-最小值)的最小值 [思路] 先横着算出每一行的长度为k的窗口内的最大值,变成一个n*(m-k+1)的矩阵mx 再竖着算出每一列的长 ...

  4. Bzoj 3831 [Poi2014]Little Bird

    3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...

  5. 【BZOJ 1233】 [Usaco2009Open]干草堆tower (单调队列优化DP)

    1233: [Usaco2009Open]干草堆tower Description 奶牛们讨厌黑暗. 为了调整牛棚顶的电灯的亮度,Bessie必须建一座干草堆使得她能够爬上去够到灯泡 .一共有N大包的 ...

  6. [BZOJ 1047] [HAOI2007] 理想的正方形 【单调队列】

    题目链接:BZOJ - 1047 题目分析 使用单调队列在 O(n^2) 的时间内求出每个 n * n 正方形的最大值,最小值.然后就可以直接统计答案了. 横向有 a 个单调队列(代码中是 Q[1] ...

  7. ●BZOJ 3831 [Poi2014]Little Bird

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3831 题解: 单调队列优化DP 定义 F[i] 为到达第i课树的疲劳值. 显然最暴力的转移就 ...

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

    Description Orez很喜欢搜集一些神秘的数据,并经常把它们排成一个矩阵进行研究.最近,Orez又得到了一些数据,并已经把它们排成了一个n行m列的矩阵.通过观察,Orez发现这些数据蕴涵了一 ...

  9. BZOJ.4182.Shopping(点分治/dsu on tree 树形依赖背包 多重背包 单调队列)

    BZOJ 题目的限制即:给定一棵树,只能任选一个连通块然后做背包,且每个点上的物品至少取一个.求花费为\(m\)时最大价值. 令\(f[i][j]\)表示在点\(i\),已用体积为\(j\)的最大价值 ...

随机推荐

  1. NoSuchMethodException <init>()

    1. Question Description: SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/n ...

  2. dapper的增、删、查改的CodeSmith模板

    <%@ Template Language="C#" TargetLanguage="Text" %> <%@ Property Name=& ...

  3. Android应用与系统安全防御

    来源:HTTP://WWW.CNBLOGS.COM/GOODHACKER/P/3864680.HTML ANDROID应用安全防御 Android应用的安全隐患包括三个方面:代码安全.数据安全和组件安 ...

  4. java微信开发(wechat4j)——发送客服消息

    微信支持主动发送客服消息.如果你要实现此功能,需要使用CustomerMsg类. 获得access_token access_token请求之后有一个过期时间,微信平台建议你使用一个中控服务器来定时刷 ...

  5. Play Framework介绍:控制器层

    业务逻辑代码通常位于模型(model)层.客户端(比如浏览器)无法直接调用其中的代码,所以模型对象提供的功能,必须作为资源以URI方式暴露给外部. 客户端使用HTTP协议来操作这些资源,从而调用了内部 ...

  6. android 不一样的学习记录

    http://blog.csdn.net/innost/article/details/48228651 ( 深入理解Android 之 Gradle) 介绍:这篇文章篇幅较长,需要有时间并足够有耐心 ...

  7. REUSE_ALV_POPUP_TO_SELECT的使用技巧

    通过函数的方法弹出一个对话框,提供选择数据的功能…… DATA: BEGIN OF lt_exidv OCCURS , box TYPE char1, exidv TYPE exidv, status ...

  8. .NET破解之百度网盘批量转存工具

    在百度网盘上看到好的资源,总想转存到自己的网盘,加以整理.由于分享的规则原因,手动转存非常麻烦,于是百度批量转存工具.最先搜到的是小兵的百度云转存助手,无需注册,试用版用户一次只能操作10个,而捐助用 ...

  9. storm学习途径

    作者: xumingming | 网址: http://xumingming.sinaapp.com/category/storm/  作者:量子恒道 | 网址:http://blog.linezin ...

  10. Python: 解决pip安装源被墙的问题

    pip install <package> -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.c ...