Codeforces round (Div. 2) C. gambling

Keywords: less iOS

Title:
That is, two people each have a list, two people take turns to operate. There are two options for each operation:
One is to select a number from your own list, add it to your score, and delete this element from the total list.
The other is to delete the other element.
Maximum difference required
Analysis:
Select the largest element in your own queue, and compare it with the largest element of the other party. Add your own large element, or delete others' elements.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
priority_queue<int,vector<int>,less<int> > q1;
priority_queue<int,vector<int>,less<int> > q2;
int main()
{
    std::ios::sync_with_stdio(false);
    int n,a,b;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a;
        q1.push(a);
    }
     for(int i=0;i<n;i++){
        cin>>b;
        q2.push(b);
    }
    //solve
    ll ans1=0,ans2=0;
    for(int i=0;i<n;i++){
        //A first
        if(q1.empty()){
            q2.pop();
        }
        else if(q2.empty()){
            ans1+=q1.top();
            q1.pop();
        }
        else{
            int t1=q1.top();
            int t2=q2.top();
            if(t1>=t2){
                ans1+=t1;
                q1.pop();
            }
            else if(t1<t2){
                q2.pop();
            }
        }

        //B second
         if(q1.empty()){
            ans2+=q2.top();
            q2.pop();
        }
        else if(q2.empty()){
            q1.pop();
        }
        else{
            int r1=q1.top();
            int r2=q2.top();
            if(r2>=r1){
                ans2+=r2;
                q2.pop();
            }
            else if(r2<r1){
                q1.pop();
            }
        }
    }
    cout<<ans1-ans2<<endl;
    return 0;
}

Posted by selenin on Fri, 03 Jan 2020 20:14:19 -0800