修剪草坪 (单调队列)

修剪草坪

这道题我们可以换一个角度思考,把题意看成:

我们找到哪些奶牛不选,且满足每两个相邻的不选的奶牛之间不能间隔超过k,当这些不选的奶牛的贡献总和最低时,我们选的奶牛贡献就最高了!

这个过程用单调队列优化一下即可,然而博主太弱了,就用了个优先队列:

\(code\):

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>

#define ll long long
#define db double
#define inf 0x7fffffff
#define rg register int
#define mp make_pair

using namespace std;

ll tot;
int n,m,a;

priority_queue<pair<ll,int> > p;

inline int qr(){
    char ch;
    while((ch=getchar())<'0'||ch>'9');
    int res=ch^48;
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+(ch^48);
    return res;
}

int main(){
    //freopen("cg.in","r",stdin);
    //freopen("cg.out","w",stdout);
    n=qr(),m=-qr();p.push(mp(0,0));
    for(rg i=1;i<=n;++i,++m){
        tot+=(a=qr());
        while(p.top().second<m)p.pop();
        p.push(mp(p.top().first-a,i));
    }while(p.top().second<m)p.pop();
    tot+=p.top().first;
    printf("%lld\n",tot);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/812-xiao-wen/p/10322071.html