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。

#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;
}
09-18 13:38