zoukankan      html  css  js  c++  java
  • luogu_P2869 [USACO07DEC]美食的食草动物Gourmet Grazers

    贪心

    第一次用multiset,感觉还有点用好像,就是个自动排序数组,log级(堆:是不是玩不起

    开始用的离散花+权值树状数组+二分,是麻烦些

    #include<iostream>
    #include<cstdio>
    
    #define ri register int
    #define u long long
    
    namespace opt {
    
        inline u in() {
            u x(0),f(1);
            char s(getchar());
            while(s<'0'||s>'9') {
                if(s=='-') f=-1;
                s=getchar();
            }
            while(s>='0'&&s<='9') {
                x=(x<<1)+(x<<3)+s-'0';
                s=getchar();
            }
            return x*f;
        }
    
    }
    
    using opt::in;
    
    #define NN 200005
    
    #include<algorithm>
    #include<set>
    
    namespace mainstay {
    
        u N,M,ans(0),now(1),t[NN],cnt,len,pre[NN],w[NN<<1];;
    
        struct node {
            u x,y;
        } a[NN],b[NN];
        
        std::multiset<u> mtst;
        
        std::multiset<u>::iterator pr;
        
        inline bool cmp(const node &x,const node &y){
            return x.y>y.y;
        }
    
        inline void solve() {
            N=in(),M=in();
            for(ri i(1); i<=N; ++i) a[i].x=in(),a[i].y=in();
            for(ri i(1); i<=M; ++i) b[i].x=in(),b[i].y=in();
            std::sort(a+1,a+N+1,cmp),std::sort(b+1,b+M+1,cmp);
            for(ri i(1); i<=N; ++i) {
                while(b[now].y>=a[i].y) mtst.insert(b[now].x),++now;
                pr=mtst.lower_bound(a[i].x);
                if(pr==mtst.end()){
                    printf("-1");
                    return;
                }
                ans+=*pr,mtst.erase(pr);
            }
            std::cout<<ans;
        }
    
    }
    
    int main() {
    
        //freopen("food.in","r",stdin);
        //freopen("food.out","w",stdout);
        std::ios::sync_with_stdio(false);
        mainstay::solve();
    
    }

     开始的写法:

    #include<iostream>
    #include<cstdio>
    
    #define ri register int
    #define u long long
    
    namespace opt {
    
        inline u in() {
            u x(0),f(1);
            char s(getchar());
            while(s<'0'||s>'9') {
                if(s=='-') f=-1;
                s=getchar();
            }
            while(s>='0'&&s<='9') {
                x=(x<<1)+(x<<3)+s-'0';
                s=getchar();
            }
            return x*f;
        }
    
    }
    
    using opt::in;
    
    #define NN 200005
    
    #include<algorithm>
    
    namespace mainstay {
    
        u N,M,ans(0),now(1),t[NN],cnt,len,pre[NN],w[NN<<1];;
    
        struct node {
            u x,y;
        } a[NN],b[NN];
    
        void add(const u &x,const u &y) {
            for(ri i(x); i<=N+M; i+=i&-i) t[i]+=y;
        }
    
        u ask(const u &x) {
            u _re(0);
            for(ri i(x); i>=1; i-=i&-i) _re+=t[i];
            return _re;
        }
    
        inline bool cmp(const node &x,const node &y) {
            return x.y>y.y;
        }
    
        void li_sanhua() {
            cnt=0;
            for(ri i(1); i<=N; ++i) w[++cnt]=a[i].x;
            for(ri i(1); i<=M; ++i) w[++cnt]=b[i].x;
            std::sort(w+1,w+cnt+1),len=std::unique(w+1,w+cnt+1)-w-1;
            for(ri i(1); i<=N; ++i) pre[std::lower_bound(w+1,w+len+1,a[i].x)-w]=a[i].x;
            for(ri i(1); i<=M; ++i) pre[std::lower_bound(w+1,w+len+1,b[i].x)-w]=b[i].x;
            for(ri i(1); i<=N; ++i) a[i].x=std::lower_bound(w+1,w+len+1,a[i].x)-w;
            for(ri i(1); i<=M; ++i) b[i].x=std::lower_bound(w+1,w+len+1,b[i].x)-w;
            cnt=0;
            for(ri i(1); i<=N; ++i) w[++cnt]=a[i].y;
            for(ri i(1); i<=M; ++i) w[++cnt]=b[i].y;
            std::sort(w+1,w+cnt+1),len=std::unique(w+1,w+cnt+1)-w-1;
            for(ri i(1); i<=N; ++i) a[i].y=std::lower_bound(w+1,w+len+1,a[i].y)-w;
            for(ri i(1); i<=M; ++i) b[i].y=std::lower_bound(w+1,w+len+1,b[i].y)-w;
        }
    
        inline void solve() {
            N=in(),M=in();
            for(ri i(1); i<=N; ++i) a[i].x=in(),a[i].y=in();
            for(ri i(1); i<=M; ++i) b[i].x=in(),b[i].y=in();
            li_sanhua(),std::sort(a+1,a+N+1,cmp),std::sort(b+1,b+M+1,cmp);
            for(ri i(1); i<=N; ++i) {
                while(b[now].y>=a[i].y) add(b[now].x,1),++now;
                u _re,_l(a[i].x),_r(N+M);
                if(!(ask(_r)-ask(_l-1))) {
                    printf("-1");
                    return;
                }
                while(_l<=_r) {
                    u mid(_l+_r>>1);
                    if((ask(mid)-ask(_l-1))>0) _r=mid-1,_re=mid;
                    else _l=mid+1;
                }
                ans+=pre[_re],add(_re,-1);
            }
            std::cout<<ans;
        }
    
    }
    
    int main() {
    
        //freopen("food.in","r",stdin);
        //freopen("food.out","w",stdout);
        std::ios::sync_with_stdio(false);
        mainstay::solve();
    
    }
  • 相关阅读:
    全面了解HTTP和HTTPS(开发人员必备)
    这几款前端必备构建工具合辑,我们帮你整理好了!
    扎心!程序员泪奔的8个瞬间
    Centos7 自定义systemctl服务脚本
    nginx配置优化+负载均衡+动静分离详解
    nginx负载均衡配置
    keepalived高可用反向代理的nginx
    Tomcat相关目录及配置文件
    tomcat快速入门
    基于keepalived双主模型的高可用LVS
  • 原文地址:https://www.cnblogs.com/ling-zhi/p/11812093.html
Copyright © 2011-2022 走看看