P3572 [POI2014]PTA-Little Bird

P3572 [POI2014]PTA-Little Bird

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


很简短的题目哼。

首先对于一个点, 他的状态一定是由前 (k) 个转移过来的。 (k) 的长度在每组询问内一定, 想到用单调队列维护 (dp)

不过此时单调队列里的元素有两个关键字: 劳累度和高度, 因为跳到比这个点高的树需要花费恒为一点体力(这个很重要), 所以我们维护单调队列的时候可以以劳累度为第一关键字, 高度为第二关键字进行比较, 为多个关键字的单调队列

解释一下为什么体力花费恒为一点很重要。 这里运用了一点贪心的思想: 此题的高度对体力消耗没有影响, 只有高和矮两种说法。 只要我的劳累值比你小, 不管你的高度有多低, 我 $ + 1$ 一定 (<=) 你, 所以可以把高度作为第二关键字比较, 从而不影响结果

Code

教训: 多次调用简单函数会大大降低算法的效率, 开一波 (O2) 才没 (T)

// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
int RD(){
    int flag = 1, out = 0;char c = getchar();
    while(c < '0' || c > '9'){if(c == '-')flag = -1;c = getchar();}
    while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
    return flag * out;
    }
const int maxn = 1000019;
int len, num ,k;
int h[maxn];
int dp[maxn];
struct Que{
    int h, val, index;
    Que (int h, int val, int index): h(h), val(val), index(index){}
    Que(){};
    bool operator < (Que const &a)const{
        if(val != a.val)return val < a.val;
        return h > a.h;
        }
    }Q[maxn];
int head, tail;
void push_back(Que x){
    while(head <= tail && x < Q[tail])tail--;
    Q[++tail] = x;
    }
void check(int x){
    while(x - Q[head].index > k)head++;
    }
int get_min(){return Q[head].val;}
int main(){
    len = RD();
    for(int i = 1;i <= len;i++)h[i] = RD();
    num = RD();
    while(num--){
        k = RD();
        head = 1, tail = 0;Q[head] = Que(1e9 + 19, 0, 0);
        memset(dp, 0, sizeof(dp));
        for(int i = 1;i <= len;i++){
            check(i);
            if(h[i] < Q[head].h)dp[i] = get_min();
            else dp[i] = get_min() + 1;
            push_back(Que(h[i], dp[i], i));
            }
        printf("%d
", dp[len]);
        }
    return 0;
    }
原文地址:https://www.cnblogs.com/Tony-Double-Sky/p/9339048.html