CF864D.Make a Permutation!
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 x__i < y__i, 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.
伊万有一个包含 n 个元素的数组,每个元素均为 1 到 n 之间的整数。
最近,伊万学习了排列及其字典序。现在,他希望以最少的修改次数(即替换最少数量的元素),将他的数组变为一个排列(即 1 到 n 中的每个整数在数组中恰好出现一次)。如果有多种方式能达到最少修改次数,他希望在这些方案中找出字典序最小的排列。
因此,最小化修改次数是第一优先级,字典序最小化是第二优先级。
为判断两个排列中哪一个字典序更小,我们逐位比较:先比较第一个元素;若相等,则比较第二个元素,依此类推。对于两个排列 x 和 y,若存在某个下标 i,使得对所有 j<i 都有 xj=yj,且 xi<yi,则称 x 字典序小于 y。
请确定伊万在完成所有修改后所得到的数组。
输入格式
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 _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ n) — the description of Ivan's array.
第一行包含一个整数 n(2≤n≤200000)—— 表示 Ivan 数组中元素的个数。
第二行包含一个整数序列 a1,a2,…,an(1≤ai≤n)—— 描述 Ivan 的数组。
输出格式
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.
第一行输出 q —— 使伊万的数组变为一个排列所需修改的最少元素个数。
第二行输出通过 q 次修改可得到的字典序最小的排列。
输入输出样例
输入#1
4 3 2 2 3
输出#1
2 1 2 4 3
输入#2
6 4 5 6 3 2 1
输出#2
0 4 5 6 3 2 1
输入#3
10 6 8 4 6 7 1 6 3 4 5
输出#3
3 2 8 4 6 7 1 9 3 10 5
说明/提示
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.
在第一个例子中,Ivan 需要将位置 1 处的数字 3 替换为数字 1,并将位置 3 处的数字 2 替换为数字 4。这样他将得到一个排列 [1, 2, 4, 3],其中仅有两个数字被修改——该排列是所有满足条件的排列中字典序最小的。
在第二个例子中,Ivan 无需进行任何修改,因为他的数组本身已经是一个排列。
输入解题思路,AI测评打分。不知道怎么写?