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 strength to fly there without any stop.

If the bird is sitting on top of the tree no. i, then in a single flight leg it can fly toany of the trees no. i+1,i+2,\cdots,i+ki+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 leastas 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 leasttiresome, 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 getfrom 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 kk.

Help all the birds, little and big!

从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制,求每次最少耗费多少体力

输入输出格式

输入格式:

There is a single integer nn (2\le n\le 1\ 000\ 0002≤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 nn integers d_1,d_2,\cdots,d_nd1​,d2​,⋯,dn​ (1\le d_i\le 10^91≤di​≤109)separated by single spaces: d_idi​ is the height of the i-th tree.

The third line of the input holds a single integer qq (1\le q\le 251≤q≤25): the number of birds whoseflights need to be planned.

The following qq lines describe these birds: in the ii-th of these lines, there is an integer k_iki​ (1\le k_i\le n-11≤ki​≤n−1) specifying the ii-th bird's stamina. In other words, the maximum number of trees that the ii-th bird can pass before it has to rest is k_i-1ki​−1.

输出格式:

Your program should print exactly qq lines to the standard output.

In the ii-th line, it should specify the minimum number of tiresome flight legs of the ii-th bird.

输入输出样例

输入样例#1: 复制

9

4 6 3 6 3 7 2 6 5

2

2

5

输出样例#1: 复制

2

1

说明

从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制,求每次最少耗费多少体力

题解

单调队列优化dp的经典题吧

先上\(O(n^2)\)方程

\(F[i]=min(F[j]+(h[j]<=h[i]));\)

很显然我们用贪心的思想。

对于 \(F[i]\) 来说,从越小的 \(F[j]\) 跳过来越好。

对于 \(h[i]\) 来说,在单调队列里面留下越大的 \(h[j]\) 越好

同时, \(F[i]\) 的单调性显然要优先于 \(h[i]\) 的单调性。

用单调队列维护两个单调性即可

以上\((j<i)\)

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
const int N=1e6+5;
int m,f[N],k,n,ch[N],q[N];
int read(){
int x=0,w=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*w;
} bool cmp(int x,int y){
return f[x]==f[y]?ch[x]>ch[y]:f[x]<f[y];
} void solve(){
k=read();
memset(f,127/3,sizeof(f));f[1]=0;//ch[0]=1e9+1;
int h=1,t=1;q[1]=1;
for(int i=2;i<=n;i++){
/*for(int j=max(0,i-k);j<i;j++){
f[i]=min(f[i],f[j]+(ch[j]<=ch[i]));
cout<<i<<' '<<f[i]<<endl;*/
while(h<=t&&q[h]<i-k)h++;
f[i]=f[q[h]]+(ch[q[h]]<=ch[i]);
while(h<=t&&cmp(i,q[t]))t--;
q[++t]=i;
}
cout<<f[n]<<endl;
} int main(){
n=read();
for(int i=1;i<=n;i++)ch[i]=read();
m=read();
while(m--)solve();
return 0;
}

[luogu]P3572 [POI2014]PTA-Little Bird(单调队列)的更多相关文章

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

    [BZOJ3831][Poi2014]Little Bird Description In the Byteotian Line Forest there are   trees in a row. ...

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

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

  3. luogu P3572 [POI2014]PTA-Little Bird

    题目描述 从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制,求每次最少耗费多少体力 单调队列优化动态规划 #include<cstdio> #include&l ...

  4. [luogu P1776] 宝物筛选 解题报告(单调队列优化DP)

    题目链接: https://www.luogu.org/problemnew/show/P1776 题目: 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF ...

  5. Luogu 2216[HAOI2007]理想的正方形 - 单调队列

    Solution 二维单调队列, 这个数组套起来看得我眼瞎... Code #include<cstdio> #include<algorithm> #include<c ...

  6. 【Luogu】P1419寻找段落(单调队列)

    题目链接 不知为何状态突然奇差无比,按说这题本来应该是水题的,但不仅不会做,还比着题解爆零五次 二分平均值(想到了),单调队列维护最大区间和(想到了但是不会,???为什么我不会???) #includ ...

  7. 【Luogu】P2219修筑绿化带(单调队列)

    题目链接 这题各种边界判断恶心死人 就是单调队列在每行求出最小的.能装进A*B方块里的花坛 然后再在刚刚求出的那个东西里面跑一遍竖着的单调队列 然后……边界调了一小时 做完这题我深刻地感觉到我又强了 ...

  8. luogu P3572 [POI2014]PTA-Little Bird |单调队列

    从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制,求每次最少耗费多少体力 #include<cstdio> #include<cstring> #i ...

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

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

随机推荐

  1. python中的and、or 操作符

    在python中 非空 非零的数都为真 1. 其"and"操作符返回的结果是决定表达式结果的值:两边条件都为真则结果为真,有一假则为假 1) 当and两边条件为“真”时,返回的是a ...

  2. Spring环境搭建及简单demo

    1. Spring框架简介(以下这段话可用于面试求职) Spring为JavaEE开发提供了一个轻量级的解决方案,主要表现为, IOC(或者叫做DI)的核心机制,提供了bean工厂(Spring容器) ...

  3. 0111MySQL优化的奇技淫巧之STRAIGHT_JOIN

    转自博客http://huoding.com/2013/06/04/261 问题 通过「SHOW FULL PROCESSLIST」语句很容易就能查到问题SQL,如下: SELECT post.* F ...

  4. BA-siemens-insight使用问题汇总

    insight安装完成后不要修改windows时间 1.如果在完成软件安装及授权后,更改了系统的时间,则软件会判断您电脑的时间已经更改,软件将无法启动,所以在软件完成正确安装后,禁止修改系统时间.更改 ...

  5. Python Study(02)之 Context Manager

    上下文管理器(context manager)是Python2.5开始支持的一种语法,用于规定某个对象的使用范围.一旦对象进入或者离开该使用范围,会有特殊操作被调用 (比如为对象分配或者释放内存).它 ...

  6. Shell简单介绍

    Shell是一种具备特殊功能的程序.它是介于使用者和linux 操作系统之核心程序(kernel)间的一个接口.为什么我们说 shell 是一种介于系统核心程序与使用者间的中介者呢?读过操作系统概论的 ...

  7. 验证DG最大性能模式下使用ARCH/LGWR及STANDBY LOG的不同情况

    总结:  --两台单实例数据库做DG,数据库版本号10.2.0.1.0 1.主库配置为:arch async,备库无STANDBY LOG. 日志中会有:RFS[4]: No standby redo ...

  8. Jmeter简单应用

    JMeter 是Apache组织的开源项目,是一个纯Java桌面应用,用于压力测试和性能测量. 1.安装jmeter jdk1.6以上下载地址:http://www.oracle.com/techne ...

  9. 并发编程网 - ifeve.com

    并发编程网 - ifeve.com 让天下没有难学的技术 首页 JAVA 深入浅出ClassLoader 深入浅出ClassLoader Dedicate to Molly. 你真的了解ClassLo ...

  10. 英语发音规则---M字母

    英语发音规则---M字母 一.总结 一句话总结: 1.M发[m]音? monkey ['mʌŋkɪ] n. 猴子:顽童 come [kʌm] vi. 来 tomato [tə'mɑːtəʊ] n. 番 ...