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)/~

Ehab and Prefix MEXs CodeForces - 1364C(思维)相关推荐

  1. Codeforces Round #649 (Div. 2)C. Ehab and Prefix MEXs[排列的构造]

    C. Ehab and Prefix MEXs 题目大意: 解题思路:题目说保证a数组是非递减的,那么如果某位置a[i]!=a[i−1]a[i]!=a[i-1]a[i]!=a[i−1]那么这个位置ii ...

  2. Ehab and Path-etic MEXs CodeForces - 1325C(思维+贪心)

    You are given a tree consisting of n nodes. You want to write some labels on the tree's edges such t ...

  3. Codeforces Round #628 (Div. 2) C. Ehab and Path-etic MEXs(贪心+思维)

    题目链接 思路:这个题做法貌似很多,我用的就是从叶子节点开始赋值,不过听大佬说的从度大于2的节点赋值也可以.比赛的时候wa test19了,特判一下n=2就行了. #include<bits/s ...

  4. C. Ehab and Prefix MEXs(构造)

    Problem - 1364C - Codeforces 题意: 给出一个长度为n的数组a,找到另一个长度为n的数组b,使得. 对于每个i(1≤i≤n)MEX({b1, b2, ..., bi})=a ...

  5. CF628 C.C. Ehab and Path-etic MEXs(思维+拓扑排序)

    CF628 C.C. Ehab and Path-etic MEXs(思维+拓扑排序) 题目链接 题意:没咋看懂,它的题解是度数最高的分配后其他的随便分配. 我做的是,从子节点往上面,依次加,拓扑排序 ...

  6. [Codeforces Round #628]1325C - Ehab and Path-etic MEXs[思维][图]

    1325C - Ehab and Path-etic MEXs[思维][图] time limit per test memory limit per test input output 1 seco ...

  7. Codeforces round #628 C.Ehab and Path-etic MEXs

    Codeforces round #628 C.Ehab and Path-etic MEXs You are given a tree consisting of n nodes. You want ...

  8. [codeforces 1325C] Ehab and Path-etic MEXs 绕不开的叶节点+特判

    Codeforces Round #628 (Div. 2)   比赛人数9400 [codeforces 1325C]  Ehab and Path-etic MEXs   绕不开的叶节点+特判 总 ...

  9. Codeforces Round #628 (Div. 2)C - Ehab and Path-etic MEXs

    C - Ehab and Path-etic MEXs 题目链接 简要描述: 从一个点到另一个点出发,所经过的边组成一个集合, 这些集合中所不包含的最小自然数为mex(u,v),要求使所有的mex(u ...

最新文章

  1. feachall php_timthumb.php详解
  2. xcode编译Undefined symbols for architecture i386问题解决
  3. Python 3.X 练习集100题 02
  4. 路由系统(urls.py)
  5. java小编程--在一个A字符串中找到与B字符串一样的,返回B字符串出现的第一个位置
  6. datax mysql replace_DataX-MySQL(读写)
  7. 函数调用 压栈的工作原理
  8. 我们如何体验 Line 的生活方式
  9. 使用PIL库使用文本生成图片(类验证码)
  10. 奔驰北京工厂两年内将再投产三款电动车;希尔顿中国市场第300家酒店开业 | 美通企业日报...
  11. 中彩分析家 打字软件
  12. sqlserver 数据库文件损坏、数据库可疑修复处理
  13. Guri团队的侧信道攻击研究
  14. u盘服务器安装win7系统安装教程视频教程,小白U盘安装win7系统教程
  15. 简单概括 文明进化的各个阶段 (39)
  16. Android 【工具类】
  17. 人脸检测技术现状及3D检测调研
  18. 第35篇 网络(五)获取本机网络信息
  19. BlueHost主机从零开始使用笔记,我踩过的坑你就不要踩了.
  20. 握手引理_图论中的握手引理–握手定理

热门文章

  1. c#中如何实现拷贝对象
  2. C#UDP通讯UdpClient
  3. db2 mysql sql server_下文给大家介绍mysql数据库介绍,具体详情如下所示:MySQL数据库:Oracle、DB2、SQL Server、MySQL、access、mang...
  4. XSS跨站脚本(web应用)——会话管理(一)
  5. wkwebview 下移20像素_UITableView嵌套WKWebView的那些坑
  6. java动态菜单显示实现,java – 为Spring MVC / AOP应用程序实现动态菜单
  7. spi四种工作模式时序图_SPI总线协议及SPI时序图详解
  8. Android开发之带行号显示的Log工具类
  9. Android开发之关于transformDexArchiveWithExternalLibsDexMergerForDebug java.lang.OutOfMemoryError问题的参考解决方案
  10. mysql存储过程的返回值在哪里设置_MySQL存储过程的返回值