洛谷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 < = d i < = 1 0 9 ) d_1,d_2...d_n(1<=d_i<=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 < = k i < = n − 1 ) k_i(1<=k_i<=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 ] < = a [ i ] ) ( i − k < = j < = i ) dp[i]=dp[j]+(a[j]<=a[i])(i-k<=j<=i) dp[i]=dp[j]+(a[j]<=a[i])(ik<=j<=i)

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

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

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


#include
#include
#include
#include
#include
#include
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;
}

你可能感兴趣的:(单调队列,动态规划--数据结构优化DP)