Codeforces Round #436 (Div. 2) D. Make a Permutation!
来源:互联网 发布:java spring书籍 编辑:程序博客网 时间:2024/06/07 03:02
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.
Examples
input43 2 2 3output21 2 4 3 input64 5 6 3 2 1output04 5 6 3 2 1 input106 8 4 6 7 1 6 3 4 5output32 8 4 6 7 1 9 3 10 5
题目大意
对于给定的序列,通过替换数字使其变成1-n的排列,问最小需要多少次替换,在此基础上,输出替换后字典序最小的序列。
解题思路
vis[]标记序列中已经出现过的数字及其出现的次数,从1-n遍历vis[],对于每个没有出现过的数字i,去替换vis[num]>1的数字(num 从1 开始)。注意若正在添加的 i 大于被替换数字且被替换数字还未发生过替换,则跳过去替换该数字出现的下一个位置(保证字典序最小),替换的同时记录替换次数。
代码实现
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>using namespace std;#define ll long long#define maxn 200007int a[maxn],vis[maxn],mark[maxn];bool flag[maxn];int main(){ int n,num; while(~scanf("%d",&n)) { num=0; memset(vis,0,sizeof(vis)); memset(mark,0,sizeof(mark)); memset(flag,0,sizeof(flag)); for(int i=0; i<n; i++) { scanf("%d",&a[i]); vis[a[i]]++; } num=0; int ans=0; int i=1; while(i<=n) { if(!vis[i]) { while(!(vis[a[num]]>1)) num++; if(!flag[a[num]]&&vis[a[num]]>1&&i>a[num]) { flag[a[num]]=1; num++; } else if(vis[a[num]]>1&&(flag[a[num]]||i<a[num])) { vis[a[num]]--; vis[i]=1; a[num]=i++; ans++; } } else i++; } printf("%d\n",ans); for(int i=0; i<n-1; i++) printf("%d ",a[i]); printf("%d\n",a[n-1]); } return 0;}
- Codeforces Round #436 (Div. 2)D. Make a Permutation! codeforces-864D. Make a Permutation!
- D. Make a Permutation! (Codeforces Round #436 (Div. 2))
- Codeforces Round #436 (Div. 2) D. Make a Permutation!
- Codeforces Round #436 (Div. 2)-贪心-D. Make a Permutation!
- Codeforces Round #436 (Div. 2) 864D.Make a Permutation!
- Codeforces Round #436 (Div. 2) D. Make a Permutation!
- Codeforces Round #436 (Div. 2) D. Make a Permutation!
- Codeforces Round #436 (Div. 2) D. Make a Permutation!(优先队列)
- Codeforces Round #436 (Div. 2) D. Make a Permutation! 思维+贪心
- Codeforces Round #436 (Div. 2) Make a Permutation!
- Codeforces Round #436 (Div. 2) Make a Permutation!
- Codeforces Round #436 (Div. 2) A.Fair Game B.Polycarp and Letters C. Bus D.Make a Permutation!
- Codeforces Round #436 D.Make a Permutation!(贪心)
- Codeforces 864D Make a Permutation!
- CodeForces 864D Make a Permutation!
- Codeforces 864 D Make a Permutation!
- Codeforces Round #209 (Div. 2) <A-D>
- Codeforces Round #231 (Div. 2)A-D
- PAT-A 1001. A+B Format (20)
- Spring Boot整合MyBatis
- 广义线性模型
- 学习Linux基本命令60个(下)
- 日常作业3
- Codeforces Round #436 (Div. 2) D. Make a Permutation!
- 扫描线(转载)
- 【LibUIDK界面库系列文章】窗口与消息
- Java main方法
- OpenCV-Python——图像阈值
- 运算符
- 三、JS【JavaScript弱类型的脚本语言】01
- CodeVS1519 过路费【Kruskal+倍增求LCA】
- 分类和逻辑回归(Classification and logistic regression)