Codeforces Round #693 (Div. 3)——D. Even-Odd Game

39 篇文章 1 订阅
5 篇文章 0 订阅

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

题目
During their New Year holidays, Alice and Bob play the following game using an array a of n 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.

题意
Alice和Bob做游戏,游戏内容是这样的:

  1. 给出一个长为n的数字序列,从Alice开始(女士优先[doge]);
  2. 每个回合每个人可以拿走一个数字。
  3. 如果是Alice,那么她拿到的偶数可以计入她的分数之中;
  4. 如果是Bob,他拿到的奇数可以计入他的分数之中;

如果双方都是1000%的状态对待游戏,那么问谁能够获胜?
(输出胜者的名字,如果平局就输出“Tie”)

在这里插入图片描述
思路
11点半之后疼的实在是不行了,有点坐不住,再加上我本来就不够聪明,这个div3打的很烂。

一开始以为是博弈论(我是fw),后来发现很简单。

将数据分成奇数列和偶数列并排序。
Alice每次拿的时候只需要看最大的偶数如果比奇数小,那就拿对方的奇数,否则拿自己的偶数;
Bob每次拿的时候只需要看最大的奇数如果比偶数小,那就拿对方的偶数,否则拿自己的奇数。

(走对方的路,让对方无路可走)

看代码,伊丽莎白!

在这里插入图片描述

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#define inf 0x3f3f3f
using namespace std;
const int maxn=2e5+100;
typedef long long ll;

ll even[maxn],odd[maxn];
int main()
{
    ll T;
    cin>>T;
    while(T--)
    {
        ll k=0,l=0;
        ll n,x;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>x;
            if(x%2==0)
                even[k++]=x;
            else
                odd[l++]=x;
        }
        sort(even,even+k);
        sort(odd,odd+l);
        ll s1=0,s2=0;
        ll f=1;
        k--,l--;
//        printf("%lld:s1=%lld  s2=%lld k=%lld l=%lld\n",f,s1,s2,k,l);
        while(k>=0||l>=0)
        {
            if(f%2)
            {
                if(k<0)
                    l--;
                else
                {
                    if(l<0||odd[l]<even[k])  //还有一种情况就是对方没有数字了,就安心拿自己的数字
                    {
                        s1+=even[k];
                        k--;
                    }
                    else
                        l--;
                }
            }
            else
            {
                if(l<0)
                    k--;
                else
                {
                    if(k<0||odd[l]>even[k])
                    {
                        s2+=odd[l];
                        l--;
                    }
                    else
                        k--;
                }
            }
//            printf("%lld:s1=%lld  s2=%lld k=%lld l=%lld\n",f,s1,s2,k,l);
            f++;
        }
        if(s1==s2)
            cout<<"Tie"<<endl;
        else if(s1<s2)
            cout<<"Bob"<<endl;
        else
            cout<<"Alice"<<endl;
    }
}

奉劝各位注意身体,否则后果堪忧!
哪个小朋友再不注意身体,就抓到灭亡迅雷站,消除u咩!

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值