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. golang(8):channel读写 & goroutine 通信

    goroutine 1.进程和线程 A. 进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的一个独立单位 B. 线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独 ...

  2. javascript是一种安全语言

    一.简单JavaScript是一个基于Java基本语句和控制流的简单而紧凑的设计,这是学习Java的一个很好的过渡.它的变量类型是弱类型,而不是严格的数据类型.二.力学JavaScript是动态的,可 ...

  3. (转)Android刷机的一些知识整理

    刷机概述刷机原因刷机可以升级和破解固件(在Android上:即可以升级系统,更改系统,获取Root权限):破解系统的原因①安装第三方软件不需要签名,不受证书的束缚:②修改系统的文件,达到系统的瘦身,以 ...

  4. SpringBoot设置首页(默认页)跳转

    SpringBoot设置首页(默认页)跳转 方案1:controller里添加一个"/"的映射路径 @RequestMapping("/")public Str ...

  5. Delphi 10.3.2来了!

    昨晚,官方正式发布了Delphi 10.3.2,增加对Mac 64应用的开发,支持Linux桌面开发,这个是通过集成fmxlinux实现的,同时修正400个bug,编译器,102个ide,84个fmx ...

  6. KVM命令记录

    创建qcow2镜像qemu-img create -f qcow2 /vm/kvm/img/vm41.img 500G 创建虚拟机virt-install --name=vm41 --disk pat ...

  7. Linux下安装opencv with-ffmpeg解决无法读取视频的问题

    1. 编译安装ffmpeg 下载源码,执行 ./configure --disable-yasm --enbale-shared --prefix=/usr/local/ffmpeg 即可. 2. 下 ...

  8. 解决Chrome无法安装CRX离线插件

    解释说明: 谷歌浏览器Chrome,版本号67.0.3396.99,自这个版本后的Chrome,手动拖放插件文件crx到谷歌浏览器,这种安装插件的方式,一定会失败,它会提示“无法从该网站添加应用,扩展 ...

  9. 代码检查工具sonarqube介绍

    SonarQube 是一款用于代码质量管理的开源工具,它主要用于管理源代码的质量.通过插件形式,可以支持众多计算机语言. 比如 java, C#, go,C/C++, PL/SQL,Cobol,Jav ...

  10. NOIP2016 Day1 T2 天天爱跑步(树上差分,LCA)

    原文链接 原题链接 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏 ...