zoukankan      html  css  js  c++  java
  • 修剪草坪 (单调队列)

    修剪草坪

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

    我们找到哪些奶牛不选,且满足每两个相邻的不选的奶牛之间不能间隔超过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
    ",tot);
        return 0;
    }
    
    
    ✐☎博主撰文不易,转载还请注明出处;若对本文有疑,请私信或在下方讨论中提出。O(∩_∩)O谢谢!☏

    ☃〔尽管小伙伴们肯定有千百种方式针对,但博主还是极其非常十分不要脸的把反对键吃掉辣!〕☃

    ✿『$At$ $last$:非常一(hu)本(shuo)正(ba)经(dao)的:博主很笨,请不要欺负他』✿✍

  • 相关阅读:
    服务器性能监控工具(MQTT协议)
    最近很烦
    开关柜无线测温数据中心后台
    合宙DTU数据流模板modbus lua脚本生成器
    4G无线-潮信报警器
    4G无线-sever酱报警器
    url_for函数——快速寻找url
    完整版模板项目实战【实战演练】
    忽略这1点,代码写的再好也没用
    不会汇报工作,工作做得再好也白瞎
  • 原文地址:https://www.cnblogs.com/812-xiao-wen/p/10322071.html
Copyright © 2011-2022 走看看