暴力 思维 Codeforces round div 2#649 C. Ehab and Prefix MEXs

该博客探讨了一种数组问题,涉及数组MEX的概念,即数组中未出现过的最小非负整数。文章介绍了如何根据给定数组a[i]动态计算b[i],使得b[i]满足特定条件。解决方案中,当a[i]与a[i-1]不同时,b[i]等于a[i-1],然后利用升序填充剩余的b[i],确保它们不在a数组中。提供的AC代码展示了完整的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意

定义 数组MEX 为数组中 没出现过的 最小非负整数
给 一个长度n的 数组ai
ai是每个前i个bi为数组 的MEX
现求b数组

思路

如果要 a[i] 和a[i-1]不同
那么必然 b[i]=a[i-1]
那么接下来考虑剩余的b[i]
只要满足 其不能是a数组有的 就可以了
剩下的可以用升序来排列
小细节是 a[n]也不能出现在b数组

AC代码

#include <bits/stdc++.h>
#define endl "\n" 
#define INF 0x3f3f3f3f3f3f3f3f
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
using namespace std;
typedef long long ll;
const  ll mod = 1e9 + 7;
const   double  PI = acos(-1.0);
const double EI = exp(1.0);
const int  N = 1e6+10;
const int maxn = 205;
const double  eps = 1e-8;
int a[N];
int b[N];
int m[N];
void solve()
{
	int  n;
	int flag = 1;
	cin >> n;
	int cnt = 0;
	memset(b, -1, sizeof(b));
	memset(m, 0, sizeof(m));
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		if (a[i] != a[i - 1])
		{
			b[i] = a[i - 1];
			m[b[i]] = 1;
		}
	}
	m[a[n]] = 1;
	for (int i = 1; i<=n; i++)
	{
		while (m[cnt])
			cnt++;
		if (b[i] == -1)
		{
			b[i] = cnt;
			m[cnt] = 1;
			cnt++;
		}
	}
	for (int i = 1; i <= n; i++)
		cout << b[i] << " ";
	cout << endl;
}
int main()
{
	std::ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	//int t; cin >> t; while (t--)
	solve();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值