Codeforces 第 934 轮[C]墨西哥游戏1题解

题目详情

这是英文原题,中文大意在下面

C. MEX Game 1

Alice and Bob play yet another game on an array 𝑎 of size 𝑛. Alice starts with an empty array 𝑐. Both players take turns playing, with Alice starting first.

On Alice's turn, she picks one element from 𝑎, appends that element to 𝑐, and then deletes it from 𝑎.

On Bob's turn, he picks one element from 𝑎, and then deletes it from 𝑎.

The game ends when the array 𝑎 is empty. Game's score is defined to be the MEX†† of 𝑐. Alice wants to maximize the score while Bob wants to minimize it. Find game's final score if both players play optimally.

†† The MEXMEX (minimum excludant) of an array of integers is defined as the smallest non-negative integer which does not occur in the array. For example:

  • The MEX of [2,2,1] is 0, because 0 does not belong to the array.
  • The MEX of [3,1,0,1] is 2, because 0 and 1 belong to the array, but 2 does not.
  • The MEX of [0,3,1,2] is 4, because 0, 1, 2 and 3 belong to the array, but 4 does not.

Input

Each test contains multiple test cases. The first line contains a single integer 𝑡𝑡 (1≤𝑡≤2⋅10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer 𝑛 (1≤𝑛≤2⋅10^5).

The second line of each test case contains 𝑛𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (0≤𝑎𝑖<𝑛).

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 2⋅10^5.

Output

For each test case, find game's score if both players play optimally.

中文大意

这个游戏是由 Alice 和 Bob 在一个包含 n 个元素的数组 a 上进行的。Alice 先手,开始时她的数组 c 是空的。游戏的规则是,每个玩家轮流进行操作:

1. Alice 在她的回合里,从数组 a 中选择一个元素,将其添加到数组 c 中,并从数组 a 中删除该元素。
2. Bob 在他的回合里,从数组 a 中选择一个元素,并将其从数组 a 中删除。

游戏直到数组 a 为空时结束。游戏的得分被定义为数组 c 的 MEX(最小排除值)。Alice 希望最大化得分,而 Bob 希望最小化得分。对于每个测试案例,你需要找到游戏最终的得分,假设两位玩家都会以最优策略进行游戏。

MEX(最小排除值):给定一个整数数组,MEX 是指不在该数组中出现的最小非负整数。

解题思路

Alice可以适应Bob的策略,并且尽可能让Bob得分最小化的行动,可以尝试防止Bob在每一轮中选择增加它的得分的元素。因此,Alice 可以采取一种策略,在每一轮中选择当前数组 a 中的最大元素,并且尽可能地选择那些不会使得数组 c 的 MEX 值增大的元素。这样做可以使得 Bob 在他的回合中难以选择那些会增加他得分的元素,从而尽量减小他的得分。

  1. 处理频率大于等于2的元素

    • 如果某个元素𝑖的频率 𝑓(𝑖)≥2,那么无论 Bob 选择移除其中的哪一个,Alice 都可以在下一步选择同样的𝑖,从而保持它们的数量不变。
    • 因此,我们可以忽略所有频率大于等于2的元素,因为它们对游戏的结果没有影响。
  2. 集中处理频率为0或1的元素

    • 现在我们只需要关注频率为0或1的元素。对于频率为0的元素,它们的 MEX 值就是它们的值。
    • 对于频率为1的元素,Alice 最多只能保留一个。如果有多个频率为1的元素,Alice 会选择其中的一个保留下来,而 Bob 会选择移除剩下的元素中最小的那个。
    • 因此,我们需要找到第一个频率为0的元素的值,如果不存在频率为0的元素,则需要找到第二个频率为1的元素的值。
  3. Alice 和 Bob 的最优策略

    • 显然,Alice 无法比数组的 MEX(第一个频率为0的元素)更好地做出选择,因为 MEX 是游戏中 Alice 可以达到的最大得分。
    • 对于频率为1的元素,Alice 可以保留其中一个,但之后 Bob 将会选择移除最小的那个频率为1的元素。这对于 Bob 来说是最优的选择,因为即使 Alice 在她的第一步中移除了其中一个频率为1的元素,Bob 也不能获得更好的选择。

代码

#include <bits/stdc++.h>
using namespace std;

int main(){
    int t; cin >> t;
    while (t--){
        int n; cin >> n;
        vector <int> f(n + 1, 0);
        
        for (int i = 0; i < n; i++){
            int x; cin >> x;
            f[x]++;
        }
        
        int c = 0;
        for (int i = 0; i <= n; i++){
            c += (f[i] == 1);
            if ((c == 2) || (f[i] == 0)){
                cout << i << "\n";
                break;
            }
        }
    }
    return 0;
}

算法分析

对于每个测试用例:
   - 统计数组中每个元素的频率,使用一个大小为 n+1 的数组 f。
   - 遍历频率数组 f,找到符合条件的元素:
     - 如果频率为0的元素存在,则直接选择该元素作为 MEX。
     - 如果频率为1的元素存在,保存第一个出现的元素,计数器 c 记录已经出现的频率为1的元素个数。
     - 如果已经出现了两个频率为1的元素,或者当前元素的频率为0,则选择该元素作为 MEX。

通过这种方式,代码实现了问题中描述的贪心策略,即选择第一个频率为0的元素作为 MEX,或者选择第二个频率为1的元素作为 MEX,以最大化 Alice 的得分并最小化 Bob 的得分。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值