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;
}
相关推荐
norlan_jame15 小时前
C-PHY与D-PHY差异
c语言·开发语言
ZPC821015 小时前
docker 镜像备份
人工智能·算法·fpga开发·机器人
ZPC821015 小时前
docker 使用GUI ROS2
人工智能·算法·fpga开发·机器人
琢磨先生David15 小时前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
哇哈哈202115 小时前
信号量和信号
linux·c++
颜酱15 小时前
栈的经典应用:从基础到进阶,解决LeetCode高频栈类问题
javascript·后端·算法
多恩Stone15 小时前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
生信大杂烩15 小时前
癌症中的“细胞邻域“:解码肿瘤微环境的空间密码 ——Nature Cancer 综述解读
人工智能·算法
czy878747515 小时前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
蜡笔小马15 小时前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost