【动态规划08】bzoj3831Little Bird(dp+单调队列)

题目描述

有一排n棵树,第i棵树的高度是Di。
MHY要从第一棵树到第n棵树去找他的妹子玩。
如果MHY在第i棵树,那么他可以跳到第i+1,i+2,…,i+k棵树。
如果MHY跳到一棵不矮于当前树的树,那么他的劳累值会+1,否则不会。
为了有体力和妹子玩,MHY要最小化劳累值。

输入输出格式

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.
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.

f[i]表示到i的最小疲劳值,转移方程很简单。
f[i]=min{f[j]+a[j]>=a[i]}(i-j<=k)
但是从时间复杂度上看,这样子是绝对会爆炸的。
所以用单调队列维护..
显然如果消耗相同的疲劳,那么高的树肯定会比低的树更好。
所以维护一个关于疲劳值的单调增队列。
之后随便搞一搞就出来了。

#include<bits/stdc++.h>
#define fer(i,j,n) for(int i=j;i<=n;i++)
#define far(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
const int maxn=1000010;
const int INF=1e9+7;
using namespace std;
/*----------------------------------------------------------------------------*/
inline int read()
{
    char ls;int x=0,sng=1;
    for(;ls<'0'||ls>'9';ls=getchar())if(ls=='-')sng=-1;
    for(;ls>='0'&&ls<='9';ls=getchar())x=x*10+ls-'0';
    return x*sng;
}
/*----------------------------------------------------------------------------*/
int n,f[maxn],a[maxn],qu,q[maxn];
inline bool cmp(int x,int y)
{
    if(f[x]<f[y])return true;
    if(f[x]>f[y])return false;
    return a[x]>=a[y];
}
int main()
{
    n=read();
    fer(i,1,n)a[i]=read();
    qu=read();
    fer(i,1,qu)
    {
        int h=1,t=1;
        int k=read();
        q[t++]=1;f[1]=0;
        fer(j,2,n)
        {
            while(h<t&&j-q[h]>k)h++;
            f[j]=f[q[h]]+(a[j]>=a[q[h]]);
            while(h<t&&cmp(j,q[t-1]))t--;
            q[t++]=j;    
        }
        printf("%d\n",f[n]);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值