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;
}
相关推荐
ch0sen1pm几秒前
加班之余从零写了个 spdlog 风格的日志库
c++
QN1幻化引擎8 分钟前
Gravity-Anchored Cognitive Field Architecture: The DalinX V8/V10 Implementation
java·前端·算法
吃着火锅x唱着歌11 分钟前
Effective C++ 学习笔记 条款33 避免遮掩继承来的名称
c++·笔记·学习
半条-咸鱼16 分钟前
【FreeRTOS】核心原理与实战速查手册
c语言·操作系统·rtos
白帽小阳21 分钟前
Typora插件开发指南:打造专属IDE式写作环境
c语言·网络·python·网络安全·github·pygame·护网行动
学计算机的计算基38 分钟前
LeetCode 图论四题精讲:BFS、拓扑排序、Trie 树的模板与优化
java·笔记·算法
浩瀚地学38 分钟前
【面试算法笔记】0202-链表-基本功能实现
java·经验分享·笔记·算法·面试
tkevinjd1 小时前
416分割等和子集
java·python·算法·leetcode·职场和发展
Keven_111 小时前
算法札记:Tarjan与拓扑序(Topo)的关系
算法·拓扑·tarjan
库克克1 小时前
【C++】类和对象--类对象模型与大小计算
开发语言·jvm·c++