【题解】Even-Odd Game【排序】

链接:https://codeforces.com/contest/1472/problem/D

D. Even-Odd Game

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

During their New Year holidays, Alice and Bob play the following game using an array aa of nn integers:

  • Players take turns, Alice moves first.
  • Each turn a player chooses any element and removes it from the array.
  • If Alice chooses even value, then she adds it to her score. If the chosen value is odd, Alice's score does not change.
  • Similarly, if Bob chooses odd value, then he adds it to his score. If the chosen value is even, then Bob's score does not change.

If there are no numbers left in the array, then the game ends. The player with the highest score wins. If the scores of the players are equal, then a draw is declared.

For example, if n=4 and a=[5,2,7,3], then the game could go as follows (there are other options):

  • On the first move, Alice chooses 2 and get two points. Her score is now 2. The array a is now [5,7,3].
  • On the second move, Bob chooses 5 and get five points. His score is now 5. The array a is now [7,3].
  • On the third move, Alice chooses 7 and get no points. Her score is now 2. The array a is now [3].
  • On the last move, Bob chooses 3 and get three points. His score is now 8. The array a is empty now.
  • Since Bob has more points at the end of the game, he is the winner.

You want to find out who will win if both players play optimally. Note that there may be duplicate numbers in the array.

Input

The first line contains an integer t (1≤t≤1e4) — the number of test cases. Then t test cases follow.

The first line of each test case contains an integer n (1≤n≤2e5) — the number of elements in the array a.

The next line contains n integers a1,a2,…,an (1≤ai≤1e9) — the array a used to play the game.

It is guaranteed that the sum of n over all test cases does not exceed 2e5.

Output

For each test case, output on a separate line:

  • "Alice" if Alice wins with the optimal play;
  • "Bob" if Bob wins with the optimal play;
  • "Tie", if a tie is declared during the optimal play.

 

 

题目大意:

有n个数字,Alice和Bob轮流取数,Alice先手。如果Alice取走的数字是偶数,那么她的分数就加上这个偶数;如果Bob取走的数字是奇数,那么他的分数就加上这个奇数。两个人都按照最优策略进行取数,分数多的人获胜,判断谁会获胜。

题目分析:

很容易发现最优策略就是每次取走最大的数。

因此只需要把数字排个序,从大到小轮流取数,再比较最后谁的分数更高即可。

代码实现:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int t;
int n;
int a[maxn];
ll sa,sb;
int pl;
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        sa=0;
        sb=0;
        pl=0;
        sort(a+1,a+n+1);
        for(int i=n;i>=1;i--){
            if(pl==0&&a[i]%2==0)sa+=a[i];
            if(pl==1&&a[i]%2==1)sb+=a[i];
            pl=1-pl;
        }

 

posted @ 2021-01-07 16:36  tzw2698  阅读(268)  评论(0)    收藏  举报