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.

贪心

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std; int n,a[],boo[],b[],c[],tot,ans; int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
boo[a[i]]++;
}
tot=;
for(int i=;i<=n;i++)
if(!boo[i]){
tot++;
b[tot]=i;
}
b[tot+]=n+;
int k=; ans=;
for(int i=;i<=n;i++)
if(boo[a[i]]>){
if(!c[a[i]]&&b[k]>a[i]){
c[a[i]]=;
continue;
}
boo[a[i]]--;
ans++;
a[i]=b[k];
k++;
}
printf("%d\n",ans);
for(int i=;i<n;i++)
printf("%d ",a[i]);
printf("%d",a[n]);
}

Codeforce D. Make a Permutation!的更多相关文章

  1. codeforce 849D. Make a Permutation!

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

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

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

  3. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  4. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  5. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  6. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  9. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

随机推荐

  1. 美团Java团队分享:如何实践支付通道自动化管理

    随着支付业务量激增,支付团队不断壮大.为了满足日益增长的业务需求,大量的支付通道逐渐接入,但由于对接的各银行和第三方系统的稳定性参差不齐,支付通道故障时有发生,作为承接上下游的核心系统,要在一系列不稳 ...

  2. 学习笔记 - 用js判断页面是否加载完成实现代码

    用document.onreadystatechange的方法来监听状态改变, 然后用document.readyState == "complete"判断是否加载完成 docum ...

  3. hihoCoder #1498 : Diligent Robots【数学】

    #1498 : Diligent Robots 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 There are N jobs to be finished. It t ...

  4. [学习OpenCV攻略][011][显示图片]

    学习资料: http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/tutorials.html   包含头文件 core.hpp:包含 ...

  5. Winform & Devexpress Chart使用入门

    一.Chart(Winform) 使用图表控件(chart)首先要理解图表区域(ChartArea).XY轴(AxisX.AxisY).数据点(Series).标题(Title).图例(Legend) ...

  6. 对python-rrdtool模块的浅研究。

    一,python-rrdtool模块安装. 切记!!! 这个rrdtool模块,在windows环境下安装太费劲,就是因为没安装上所以现在改成了在ubuntu环境下开发,原来没有体会过,现在可真是体会 ...

  7. 程序员是这样区分Null和Undefined

    Null类型 Null类型是第二个只有一个值的数据类型,这个特殊的值是null.从逻辑角度来看,null值表示一个空对象指针,而这也正是使用typeof操作符检测null值时会返回"obje ...

  8. 基于 fireasy 构建的 asp.net core 示例

    最近花时间弄了一个关于fireasy使用的demo,已放到 github 上供大家研究,https://github.com/faib920/zero 该 demo 演示了如何使用 fireasy 创 ...

  9. 【Linux搭建创建FTP服务器】---完美解决 - 费元星

    配置大红字文件置顶:   vsftp d.conf 4.5KB     useradd -d /home/bai_du -s /sbin/nologin bai_du 修改访问权限: usermod ...

  10. 解决:mysql is blocked because of many connection errors;

    标签:because service foreign errors closed 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http:// ...