其他分享
首页 > 其他分享> > gym102201E_Eat Economically

gym102201E_Eat Economically

作者:互联网

题意

给\(2n\)个物品,分别有\(a,b\)属性,对于\(i=1...n\),选择\(i\)个\(a\)属性和\(i\)个\(b\)属性,且每个物品只能作为一种属性的贡献,求最小的值。

分析

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+50;
struct node{
    int id;
    ll val,sec;
    bool operator <(const node& rhs)const{
        if(val!=rhs.val){
            return val>rhs.val;
        }else{
            return sec-val>rhs.sec-rhs.val;
        }
    }
};
priority_queue<node> lc,dn,ltd,dtl;
int n,vis[N];
ll l[N],d[N];
int main(){
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=2*n;i++){
        scanf("%lld%lld",&l[i],&d[i]);
        lc.push(node{i,l[i],d[i]});
        dn.push(node{i,d[i],l[i]});
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        while(!lc.empty() && vis[lc.top().id]){
            lc.pop();
        }
        if(lc.empty()){
            auto t=dtl.top();
            dtl.pop();
            ans+=t.val;
            auto g=dn.top();
            dn.pop();
            ans+=g.val;
            ltd.push(node{t.id,d[t.id]-l[t.id]});
            dtl.push(node{g.id,l[g.id]-d[g.id],0});
        }else{
            auto a=lc.top();
            while(vis[a.id]){
                lc.pop();
                a=lc.top();
            }
            while(!dn.empty() && vis[dn.top().id]){
                dn.pop();
            }
            if(dtl.empty() || dn.empty()){
                ans+=a.val;
                lc.pop();
                vis[a.id]=1;
                ltd.push(node{a.id,d[a.id]-l[a.id],0});
            }else{
                auto b=dtl.top();
                auto c=dn.top();
                if(a.val<=b.val+c.val){
                    ans+=a.val;
                    lc.pop();
                    vis[a.id]=1;
                    ltd.push(node{a.id,d[a.id]-l[a.id],0});
                }else{
                    ans+=b.val+c.val;
                    dtl.pop();
                    dn.pop();
                    vis[c.id]=1;
                    ltd.push(node{b.id,d[b.id]-l[b.id],0});
                    dtl.push(node{c.id,l[c.id]-d[c.id],0});
                }
            }
        }
        while(!dn.empty() && vis[dn.top().id]){
            dn.pop();
        }
        if(dn.empty()){
            auto t=ltd.top();
            ltd.pop();
            ans+=t.val;
            auto g=lc.top();
            lc.pop();
            ans+=g.val;
            dtl.push(node{t.id,l[t.id]-d[t.id],0});
            ltd.push(node{g.id,d[g.id]-l[g.id],0});
        }else{
            auto a=dn.top();
            while(vis[a.id]){
                dn.pop();
                a=dn.top();
            }
            while(!lc.empty() && vis[lc.top().id]){
                lc.pop();
            }
            if(ltd.empty() || lc.empty()){
                ans+=a.val;
                dn.pop();
                vis[a.id]=1;
                dtl.push(node{a.id,l[a.id]-d[a.id],0});
            }else{
                auto b=ltd.top();
                auto c=lc.top();
                if(a.val<=b.val+c.val){
                    ans+=a.val;
                    dn.pop();
                    vis[a.id]=1;
                    dtl.push(node{a.id,l[a.id]-d[a.id],0});
                }else{
                    ans+=b.val+c.val;
                    ltd.pop();
                    lc.pop();
                    vis[c.id]=1;
                    dtl.push(node{b.id,l[b.id]-d[b.id],0});
                    ltd.push(node{c.id,d[c.id]-l[c.id],0});
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

标签:val,反悔,int,最小,gym102201E,Economically,花费,Eat,属性
来源: https://www.cnblogs.com/zxcoder/p/11609462.html