Codeforce D. Even-Odd Game (Game + Thinking + Greed)

Topic link

Title:

Alice and Bob have n integers in array a, choose numbers

If Alice chooses an even number, then this number is added to Alice's score. If Alice chooses an odd number, no score is counted.

If Bob chooses an odd number, then this number is added to Bob’s minutes, if he chooses an even number, no points are counted

Both adopt the optimal strategy, Alice goes first, the one with more points wins, and if it is a tie, Tie is output.

Ideas

The best solution is to maximize yourself on the one hand, and minimize the opponent on the other.

This determines that we will look at the largest one. Look at Alice first. He wants an even number, so if the largest is an even number, then Alice will take it directly. Otherwise, it will be an odd number. Alice Villa will not let Bob not take the largest, so she gives Alice won't get a bonus if he takes this.

The same is true for Bob, he wants an odd number, and the biggest one is to take the odd number directly, not also to prevent Alice from taking it.

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5+10;
typedef long long ll;
ll a[maxn];
int main() {
    
    
    ll t;
    cin>>t;
    while(t--){
    
    
        ll n,m;
        cin>>n;
        for(int i=1;i<=n;i++){
    
    
            scanf("%lld",&a[i]);
        }
        sort(a+1,a+1+n);
        ll sum=0,ans=0,flag=0;
        for(int i=n;i>=1;i--){
    
    
            if(flag%2){
    
    //b  j
                if(a[i]%2){
    
    
                    ans+=a[i];
                }
            }else {
    
    ///a   o
                if(a[i]%2==0){
    
    
                    sum+=a[i];
                }
            }
            flag++;
        }
        if(sum>ans) puts("Alice");
        else if(sum==ans) puts("Tie");
        else puts("Bob");

    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45911397/article/details/114826547