C. Salyg1n and the MEX Game Codeforces Round 897 (Div. 2)

Problem - C - Codeforces

题目大意:有一个所有数互不相同的长度为n的数组n,A先手,可以向数组中假如一个没有的数x,B可以从数组中移除一个<x的数,目标是使数组最终的MEX最小,扮演A进行操作,使数组最终的MEX最大

1<=n<=1e5;最大回合数为n

思路:我们要让当前数组的MEX增大,必须放入MEX,然后B移除一个更小的数以后,MEX肯定会变小,所以我们必须把B去掉的数加回去来维持MEX,这样的话每回合操作的数越来越小,因为游戏规定B不能操作就直接游戏结束,所以如果在某一回合A放了一个0,那么当前游戏就结束了,也就是除了A的第一次操作以外,其他操作都只能维护原有的MEX。

cpp 复制代码
#include<bits/stdc++.h>
//#include<__msvc_all_public_headers.hpp>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int n;
int a[N];
void init()
{
	
}
void solve()
{
	cin >> n;
	init();
	int mex = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	sort(a + 1, a + n + 1);//将原数组排序
	for (int i = 1; i <= n; i++)
	{//找MEX
		if (a[i] == mex)
			mex++;
		else
			break;
	}
	cout << mex << endl;
	cout.flush();
	int op;
	while (cin >> op)
	{
		if (op < 0)
		{
			break;
		}
		cout << op << endl;//B拿走什么,A就放回什么
		cout.flush();
	}
}
int main()
{
	cin.tie(0);
	cout.tie(0);

	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}
相关推荐
Smart-Space4 分钟前
将copilot键映射为右control - 参考powertoys实现
c++
Trouvaille ~8 分钟前
【Linux】TCP协议基础与连接管理详解:从三次握手到四次挥手
linux·运维·服务器·网络·c++·网络协议·tcp/ip
想做功的洛伦兹力121 分钟前
2026/2/12日打卡
开发语言·c++·算法
大模型玩家七七23 分钟前
技术抉择:微调还是 RAG?——以春节祝福生成为例
android·java·大数据·开发语言·人工智能·算法·安全
你撅嘴真丑24 分钟前
蛇形填充数组 与 查找最接近的元素
数据结构·c++·算法
Mr YiRan28 分钟前
指针实战运用进阶之字符串操作
c语言
JXL186036 分钟前
Dynamic programming
算法
blackicexs1 小时前
第四周第四天
数据结构·c++·算法
TracyCoder1231 小时前
LeetCode Hot100(46/100)——74. 搜索二维矩阵
算法·leetcode·矩阵
foundbug9991 小时前
果蝇优化算法(FOA)详解:原理、实现与应用
算法