D. Make a Permutation!
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.

Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.

Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.

In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.

Determine the array Ivan will obtain after performing all the changes.

Input

The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array.

The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array.

Output

In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.

Examples
input
  1. 4
    3 2 2 3
output
  1. 2
    1 2 4 3
input
  1. 6
    4 5 6 3 2 1
output
  1. 0
    4 5 6 3 2 1
input
  1. 10
    6 8 4 6 7 1 6 3 4 5
output
  1. 3
    2 8 4 6 7 1 9 3 10 5
Note

In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable.

In the second example Ivan does not need to change anything because his array already is a permutation.

算法:思维

题意:给你一个长度为n的数组,里面有重复的元素,你需要把这个多余的重复元素改成1 ~ n中那些你没有用过的元素,问你需要更改多少次,以及最小的元素序列是什么?

思路:首先,我先将那些多余的重复元素和那些没有用过的元素记录下来,然后就进行遍历判断。假如当前元素时重复元素,并且当前元素比未使用过的最小元素大的话,就将其覆盖,否则,你就需要记录当前元素已经使用,当之后在遇到这个元素的时候,我就可以直接覆盖。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <set>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. #define INF 0x3f3f3f3f
  10. typedef long long ll;
  11.  
  12. const int maxn = 2e5+;
  13.  
  14. int vis[maxn];
  15. int v[maxn];
  16. int n;
  17. int arr[maxn];
  18. int b[maxn];
  19.  
  20. int main() {
  21. scanf("%d", &n);
  22. int k = ;
  23. for(int i = ; i <= n; i++) {
  24. scanf("%d", &arr[i]);
  25. vis[arr[i]]++; //记录使用过的每个元素的个数
  26. }
  27. for(int i = ; i <= n; i++) {
  28. if(!vis[i]) {
  29. b[k++] = i; //记录那些没有使用的元素
  30. }
  31. }
  32. int j = ;
  33. for(int i = ; i <= n; i++) {
  34. int x = arr[i];
  35. if(vis[x] > ) { //当使用过的元素有多个时
  36. if(b[j] < x) { //如果当前未使用的最小元素比其小,那么直接覆盖
  37. arr[i] = b[j++];
  38. vis[x]--;
  39. } else {
  40. if(v[x]) { //当前元素已被记录,在它前面有个和它一样的
  41. arr[i] = b[j++];
  42. }
  43. v[x]++;
  44. }
  45.  
  46. }
  47. }
  48. printf("%d\n", k);
  49. for(int i = ; i <= n; i++) {
  50. printf("%d%c", arr[i], " \n"[i == n]);
  51. }
  52. return ;
  53. }

D. Make a Permutation!(思维)的更多相关文章

  1. MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值

    F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...

  2. leetcode 484. Find Permutation 思维题

    https://leetcode.com/contest/leetcode-weekly-contest-16a/problems/find-permutation/ 设原本的数字是0,那么按照它的D ...

  3. Codeforces 102394I Interesting Permutation 思维

    题意: 你有一个长度为n的序列a(这个序列只能使用[1,n]区间内的数字,每个数字只能使用一次),通过a序列可以构造出来三个相同长度的序列f.g.h For each 1≤i≤n, fi=max{a1 ...

  4. permutation 2(递推 + 思维)

    permutation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  5. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

  6. TOJ 2130: Permutation Recovery(思维+vector的使用)

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2130 时间限制(普通/Java): ...

  7. Permutation(构造+思维)

    A permutation p is an ordered group of numbers p1,   p2,   ...,   pn, consisting of ndistinct positi ...

  8. hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs

    题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...

  9. codeforce 436 D贪心思维题Make a Permutation!

    D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

随机推荐

  1. 深入理解计算机系统 第十一章 网络编程 part2 第二遍

    客户端和服务器通过因特网这个全球网络来通信.从程序员的观点来看,我们可以把因特网看成是一个全球范围的主机集合,具有以下几个属性: 1.每个因特网主机都有一个唯一的 32 为名字,称为它的 IP 地址 ...

  2. node 环境安装

    记录一下, 方便自己需要时用, 免得到处找 1. 官网下载安装node(选择LTS长期支持版本), 一路点击next即可(傻瓜式安装) 2. 验证是否正确安装, 打开命令窗口, 执行 node -v ...

  3. winform messageBox.Show()

    MessageBox.Show(" 5 个参数...... ",     " 亮仔提示",     MessageBoxButtons.OKCancel,    ...

  4. 前段js实时判断会话是否超时

    前端自行判断页面是否超时 jsp从后台获取到回话时间var sessionTime="${sessionTime}"; js中 //实时判断会话是否超时 var lastSessi ...

  5. Json-server在Vue 2.0中使用--build文件中没有dev-server文件

    跟大佬的视频使用json-server模拟后台数据调用,发现build文件中并没有dev-server.js. 新版的vue-cli取消了dev-server.js和dev-client.js   改 ...

  6. 记录一则ORA

    应用服务器:Windows Server 2008 R2 Enterprise故障现象:项目侧同事反映应用服务器上的程序连接数据库报错:ORA-12560: TNS: 协议适配器错误 1.故障重现 2 ...

  7. Java学习笔记【三、运算符、表达式、语句】

    运算符 算数运算符 /* / % ++ -- 关系运算符 == != > /< >= /<= 位运算符 &(按位与,有0是0,否则1) |(按位或,有1是1,否则0) ...

  8. PHP删除字符串中的空格和换行符 将字符串中的连续多个空格转换为一个空格

    //删除空格和回车 function trimall($str){ $qian=array(" "," ","\t","\n&qu ...

  9. Oracle【序列、索引、视图、分页】

    1.Oracle序列语法:create sequence 序列名 特点1:默认是无值,指针指向没有值的位置 特点2:序列名.nextval 每次执行值会自增一次,步长为 1 特点3:序列名.currv ...

  10. ARM cortex-version

    cortex-M\A\R M microcontroller 微控制器   就是单片机 A application    应用及处理器   就是手机平板电脑等 R realtime 实时处理器  响应 ...