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. React Native分享第三方遇到的问题

    这几天做APP的分享,好多东西都不懂,踩了好多坑,所幸现在做好了,开心^_^ 时间紧,随便记一下先. 1.签名文件要和各平台的签名一致 2.新浪微博和微信的分享要打包之后,加上签名了才可以(我一直不知 ...

  2. strace命令【转】

    strace命令使用: strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用 ...

  3. 从好用到更好用 —— 2017 年又拍云 CDN 功能更新回顾

    又拍云一直致力于为客户带来更好的服务,在 2017 年又拍云 CDN 服务进行了数次重大更新,在功能上更加全面.完善,进一步提升了 CDN 的稳定性与安全性. 在过去一年里又拍云 CDN 服务共进行了 ...

  4. 基于Windows下python环境变量配置

    方法和Java环境变量配置是一样的,不懂的请移步这里 虽然这样说,还是唠唠叨叨几句吧QAQ 默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行直接使 ...

  5. [FZU1977] Pandora adventure

    来学插头DP了= = GDKOI前觉得不会考数位DP,GDOI前觉得插头DP用不上.. 结果令人伤感>_< 这题并不用增加状态.. 只要在形成环的时候,让形成环的位置在最后一个必走点之后, ...

  6. BZOJ1008: [HNOI2008]越狱-快速幂+取模

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8689  Solved: 3748 Description 监狱有 ...

  7. Linux系统常用命令权威指南

    <一>线上查询及帮助命令(2)1.man man [选项] [命令] 查看命令帮助,命令的词典,更复杂的还有info,但不常用. #man cd-a 显示所有的手册页,不只是显示第一个-f ...

  8. Spring框架学习笔记(1)——HelloWorld

    1.创建一个新的项目,并添加Spring框架 2.创建HelloWorld.java package com.broadtext.beans.helloworld; public class Hell ...

  9. CentOS 6.5 搭建 Zabbix

    CentOS 6.5 搭建 Zabbix 说明: 操作系统:CentOS 6.5 IP地址:192.168.21.127 Web环境:Nginx+MySQL+PHP zabbix版本:Zabbix 2 ...

  10. 微信小程序监听input输入并取值

    小程序的事件分为两种,冒泡和非冒泡事件,像<form/>的submit事件,<input/>的input事件,<scroll-view/>的scroll事件等非冒泡 ...