单调队列错了可以看看

P2627 [USACO11OPEN] Mowing the Lawn G

zo__Oz @ 2022-08-17 10:19:01

这里是根据题解区中的dalao们逆推+优先队列的思路的完形填空

#include<bits/stdc++.h>
using namespace std;
#define rt register int
int n,k,hh,tt;
int e[100005],q[100005];
long long dp[100005],sum;
int main()
{
    scanf("%d%d",&n,&k);
    for(rt i=1;i<=n;++i)
        scanf("%d",&e[i]),sum+=e[i];    
    for(rt i=1;i<=n+1;++i)dp[i]=9999999999999999;
    hh=tt=1;
    for(rt i=1;i<=n+1;++i){
        while(q[hh]<i-k-1&&hh<=tt)hh++;
        dp[i]=min(dp[q[hh]]+e[i],dp[i]);
        while(dp[i]<dp[q[tt]]&&hh<=tt)tt--;
        q[++tt]=i;
    }
    printf("%lld\n",sum-dp[n+1]);
    return 0;
}

|