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
4
3 2 2 3
output
2
1 2 4 3
input
6
4 5 6 3 2 1
output
0
4 5 6 3 2 1
input
10
6 8 4 6 7 1 6 3 4 5
output
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中那些你没有用过的元素,问你需要更改多少次,以及最小的元素序列是什么?

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

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map> using namespace std; #define INF 0x3f3f3f3f
typedef long long ll; const int maxn = 2e5+; int vis[maxn];
int v[maxn];
int n;
int arr[maxn];
int b[maxn]; int main() {
scanf("%d", &n);
int k = ;
for(int i = ; i <= n; i++) {
scanf("%d", &arr[i]);
vis[arr[i]]++; //记录使用过的每个元素的个数
}
for(int i = ; i <= n; i++) {
if(!vis[i]) {
b[k++] = i; //记录那些没有使用的元素
}
}
int j = ;
for(int i = ; i <= n; i++) {
int x = arr[i];
if(vis[x] > ) { //当使用过的元素有多个时
if(b[j] < x) { //如果当前未使用的最小元素比其小,那么直接覆盖
arr[i] = b[j++];
vis[x]--;
} else {
if(v[x]) { //当前元素已被记录,在它前面有个和它一样的
arr[i] = b[j++];
}
v[x]++;
} }
}
printf("%d\n", k);
for(int i = ; i <= n; i++) {
printf("%d%c", arr[i], " \n"[i == n]);
}
return ;
}

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. 解决VS2005打开js,css,asp.php等文件,中文都是乱码的问题

    用记事本打开可以正常观看但是用VS2005编辑器打开JS,中文确实乱码. 解决办法:在VS 2005 的设置里面选择自动检测Utf-8:“工具”->“选项”->“文本编辑器”->“自 ...

  2. [转载]GridView中点击某行的任意位置就选中该行

    原文链接:http://www.cnblogs.com/Echo529/p/4521701.html GridView中点击某行的任意位置就选中该行 分类: 第一步:添加选择列 点击GridView右 ...

  3. Pytorch中randn和rand函数的用法

    Pytorch中randn和rand函数的用法 randn torch.randn(*sizes, out=None) → Tensor 返回一个包含了从标准正态分布中抽取的一组随机数的张量 size ...

  4. 向PHP发送HTTP-Get请求

    1.get.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  5. [Android] Installation failed due to: ''pm install-create -r -t -S 4590498' returns error 'UNSUPPORTED''

    小米特有问题 关闭开发者选项的启用MIUI优化 不得不说, 这是真的坑...

  6. php扩展库解释

    扩展库 说明 注解 php_bz2.dll bzip2 压缩函数库 无 php_calendar.dll 历法转换函数库 自 PHP 4.0.3 起内置 php_cpdf.dll ClibPDF 函数 ...

  7. 使用webpack + momentjs时, 需要注意的问题

    注意开发HTML页面charset, 如是不是utf-8, 比如是shift_jis,  一般会在webpack里用插件EncodingPlugin把开发的utf-8格式转码成shift_jis格式 ...

  8. Spring整合Hessian的使用

    该文章转赞自  https://www.cnblogs.com/ontheroad_lee/p/3797239.htm 个人感觉写的非常好,刚学习,先记录下来 1.1     Hessian简介 He ...

  9. Centos7查不出ip地址

    今天遇到了这个问题,解决后记录一下: //输入查询命令 ifconfig或者ip addr 如图,是显示不出信息的 找到ens33的配置文件,输入命令 vi /etc/sysconfig/networ ...

  10. 如果在docker中部署tomcat,并且部署java应用程序

    1.先说如何在docker中部署tomcat 第一步:root用户登录在系统根目录下创建文件夹tomcat7,命令如:mkdir tomcat7,并且切换到该目录下:cd tomcat7: 第二步:创 ...