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;
}
相关推荐
handler014 小时前
从源码到二进制:深度拆解 Linux 下 C 程序的编译与链接全流程
linux·c语言·开发语言·c++·笔记·学习
t***5445 小时前
如何在Dev-C++中使用Clang编译器
开发语言·c++
Qbw20045 小时前
【Linux】进程地址空间
linux·c++
Aurorar0rua5 小时前
CS50 x 2024 Notes C - 05
java·c语言·数据结构
棋子入局6 小时前
C语言制作消消乐游戏(2)
c语言·开发语言·游戏
6Hzlia6 小时前
【Hot 100 刷题计划】 LeetCode 739. 每日温度 | C++ 逆序单调栈
c++·算法·leetcode
良木生香6 小时前
【C++初阶】:STL——String从入门到应用完全指南(1)
c语言·开发语言·数据结构·c++·算法
XWalnut7 小时前
LeetCode刷题 day16
数据结构·算法·leetcode·链表·动态规划
Kurisu_红莉栖7 小时前
c++复习——const,static字
c++