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

时空限制 1000ms / 128MB

题目描述

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!

输入格式:

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 <= 10 9 ) ,separated by single spaces: d i 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 whoseflights need to be planned.

The following q lines describe these birds: in the   i t h   of these lines, there is an integer k i ( 1 <= k i <= n 1 ) specifying the   i t h   bird’s stamina. In other words, the maximum number of trees that the   i t h   bird can pass before it has to rest is k i 1

输出格式:

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

In the   i t h   line, it should specify the minimum number of tiresome flight legs of the   i t h   bird.


题目分析

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

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

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

猜你喜欢

转载自blog.csdn.net/niiick/article/details/80807428