Ehab and Prefix MEXs CodeForces - 1364C(思维)

Given an array a of length n, find another array, b, of length n such that:

for each i (1≤i≤n) MEX({b1, b2, …, bi})=ai.
The MEX of a set of integers is the smallest non-negative integer that doesn’t belong to this set.

If such array doesn’t exist, determine this.

Input
The first line contains an integer n (1≤n≤105) — the length of the array a.

The second line contains n integers a1, a2, …, an (0≤ai≤i) — the elements of the array a. It’s guaranteed that ai≤ai+1 for 1≤i<n.

Output
If there’s no such array, print a single line containing −1.

Otherwise, print a single line containing n integers b1, b2, …, bn (0≤bi≤106)

If there are multiple answers, print any.

Examples
Input
3
1 2 3
Output
0 1 2
Input
4
0 0 0 2
Output
1 3 4 0
Input
3
1 1 3
Output
0 2 1
Note
In the second test case, other answers like [1,1,1,0], for example, are valid.
思路:这个题目卡了好久,一开始思路没有正确。
我们可以想一下,最终数组中的值,一定是[0,n-1].
我们记录一下a数组中[0,n-1]没有出现过的值,保存在数组c中。
如果a[i]==a[i-1]的话,那我们就在c数组中找一个最小的数放置到i这个位置。
如果a[i]≠a[i-1]的话,那我们就将a[i-1]添加到i这个位置。
为什么这样对呢?
如果a[i]≠a[i-1],对于a[i-1]这个数字,在i-1之前是没有出现过的,那么在第i个位置出现a[i-1]这个数字就没有问题。
如果a[i]==a[i-1]的话,在第i个位置放置一个a这个数组中没有出现过的数字,对目前的结果是没有影响的。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e5+100;
int a[maxx];
int b[maxx];
int vis[maxx];
int n;

int main()
{
	int t;
	scanf("%d",&n);
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++) scanf("%d",&a[i]),vis[a[i]]=1;
	int cnt=0;
	for(int i=n;i>=0;i--) if(!vis[i]) b[++cnt]=i;
	a[0]=a[1];
	for(int i=1;i<=n;i++)
	{
		if(a[i]==a[i-1]) cout<<b[cnt--]<<" ";
		else cout<<a[i-1]<<" ";
	}
	cout<<endl;
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值