修剪草坪 (单调队列)

修剪草坪

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

我们找到哪些奶牛不选,且满足每两个相邻的不选的奶牛之间不能间隔超过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;
}

转载于:https://www.cnblogs.com/812-xiao-wen/p/10322071.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值