洛谷P3572 [POI2014]PTA-Little Bird【单调队列优化DP】

时空限制 1000ms / 128MB

题目描述

In the Byteotian Line Forest there are n 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 , ⋯   , i + k i+1,i+2,\cdots,i+k 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 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 k .

Help all the birds, little and big!

输入格式:

There is a single integer$(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 n integers d 1 , d 2 . . . d n ( 1 &lt; = d i &lt; = 1 0 9 ) d_1,d_2...d_n(1&lt;=d_i&lt;=10^9) d1,d2...dn(1<=di<=109),separated by single spaces: d i ​ d_i​ diis the height of the i-th tree.

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

The following q q q lines describe these birds: in the i − t h i -th ithof these lines, there is an integer k i ( 1 &lt; = k i &lt; = n − 1 ) k_i(1&lt;=k_i&lt;=n-1) ki(1<=ki<=n1)specifying the i − t h i -th ithbird’s stamina. In other words, the maximum number of trees that the i − t h i -th ithbird can pass before it has to rest is k i − 1 k_{i-1} ki1

输出格式:

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

In the$\ i -th\ l i n e , i t s h o u l d s p e c i f y t h e m i n i m u m n u m b e r o f t i r e s o m e f l i g h t l e g s o f t h e line, it should specify the minimum number of tiresome flight legs of the line,itshouldspecifytheminimumnumberoftiresomeflightlegsofthe\ i -th\ $bird.


题目分析

d p [ i ] dp[i] dp[i]表示从1跳到i需要的最少体力
那么状态转移方程为
d p [ i ] = d p [ j ] + ( a [ j ] &lt; = a [ i ] ) ( i − k &lt; = j &lt; = i ) dp[i]=dp[j]+(a[j]&lt;=a[i])(i-k&lt;=j&lt;=i) dp[i]=dp[j]+(a[j]<=a[i])(ik<=j<=i)

观察题目数据范围
不难想但要用单调队列维护最小值

但是到这里其实还有一个问题
就是 d p [ j ] ( i − k &lt; = j &lt; i ) dp[j](i-k&lt;=j&lt;i) dp[j](ik<=j<i)中可能有相同的dp值
但其中一棵的高度大于第i棵,另一棵小于第i棵
直接的单调队列维护有可能会使高度较小的那棵到达队首而使得 d p [ i ] dp[i] dp[i]不是最优

所以我们在判断队尾是否出时时要双关键字检查
若队尾dp值大于当前dp值,直接弹出队尾
若队尾dp值等于当前dp值且队尾对应的树高度小于当前高度,直接弹出队尾


#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=1000010;
int n,m;
int a[maxn];
int q[maxn],ll,rr;
int dp[maxn];

int main()
{
    n=read(); a[0]=1e9;
    for(int i=1;i<=n;++i)a[i]=read();
    m=read();
    while(m--)
    {
        int k=read(); 
        ll=rr=1; q[ll]=0;
        for(int i=1;i<=n;++i)
        {
            while(ll<rr&&q[ll]<i-k)++ll;
            dp[i]=dp[q[ll]]; if(a[q[ll]]<=a[i])dp[i]++;
            while(ll<rr&&( dp[q[rr-1]]>dp[i] || (dp[q[rr-1]]==dp[i]&&a[q[rr-1]]<a[i]) ))--rr;
            q[rr++]=i;
        }
        printf("%d\n",dp[n]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值