[Codeforces 864D]Make a Permutation!

Description

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.

Sample Input

4
3 2 2 3

Sample Output

2
1 2 4 3

HINT

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.

题解

又掉进了题意的坑里...

一开始以为更换数字的代价是前后数字的差值...结果发现更换代价就是$1$...水得不能再水...

那显然最小代价就是把多余的数字变成没有的数字。

对于序列的字典序,也很简单,我们玩一个小贪心,显然数字越小要放越前面。我们将没有的数字从大到小压入栈中。

从$1$~$n$遍历序列,若扫到重复的元素,如果栈顶元素比它小,显然要替换。若比它大,我们选择跳过(虽然只能跳过一次,但先跳总比后跳好),记录一下,防止之后重复跳两次。

 //It is made by Awson on 2017.9.29
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = ;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
} int n, a[N+], cnt[N+];
bool skip[N+];
stack<int>S; void work() {
read(n);
for (int i = ; i <= n; i++) {
read(a[i]);
cnt[a[i]]++;
}
for (int i = n; i >= ; i--)
if (!cnt[i]) S.push(i);
printf("%d\n", S.size());
for (int i = ; i <= n; i++) {
if (cnt[a[i]] > ) {
if (skip[a[i]] || a[i] > S.top()) {
cnt[a[i]]--;
a[i] = S.top();
S.pop();
}
else skip[a[i]] = ;
}
}
for (int i = ; i <= n; i++) printf("%d ", a[i]);
}
int main() {
work();
return ;
}
上一篇:mapreduce 关于小文件导致任务缓慢的问题


下一篇:通过工厂方式配置bean