codeforces contest 864 problemD
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.
InputThe 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.
OutputIn 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 input4output
3 2 2 3
2input
1 2 4 3
6output
4 5 6 3 2 1
0input
4 5 6 3 2 1
10output
6 8 4 6 7 1 6 3 4 5
3Note
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.
————————————————————————————————————
这道题呢就是叫你用最少的修改次数使得序列变成一个n的排列 在这个前提下使得字典序最小
我们可以记录一下每个数出现的次数 把没出现的从小到大扔进队列里面 然后扫一边原序列
如果当前数字出现的次数大于1我们可以考虑是否修改 如果他这个值之前已经出现过并且没有修改
那么这个位置就必须修改了 不然的话 如果他的值比当前队列的值要小 那就不修改 留着之后修改这样字典序一定最小
打个标记就可以辣
#include<cstdio> #include<cstring> #include<algorithm> const int M=250007; int read(){ int ans=0,f=1,c=getchar(); while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();} return ans*f; } int n,k,ansh; int f[M],q[M],cnt; int sum,ans[M],last[M]; struct pos{int v,x;}e[M]; bool cmp(pos a,pos b){return a.x<b.x;} int main(){ n=read(); for(int i=1;i<=n;i++){k=read(); f[k]++; ans[i]=k;} for(int i=1;i<=n;i++) if(!f[i]) q[++cnt]=i; int k=1; for(int i=1;i<=n;i++){ if(f[ans[i]]>1){ if(last[ans[i]]||q[k]<ans[i]) f[ans[i]]--,ans[i]=q[k++],ansh++; else last[ans[i]]=1; } } printf("%d\n",ansh); for(int i=1;i<=n;i++) printf("%d ",ans[i]); return 0; }View Code
- codeforces——contest 864 problemE
- codeforces contest 479
- codeforces 659B B. Qualifying Contest(水题+sort)
- CodeForces 659 B. Qualifying Contest(结构体排序的问题)
- codeforces contest 733
- CodeForces - 659B Qualifying Contest (模拟)水
- codeforces 2016-2017 TW Wrold Final Contest J. Zero Game
- Codeforces contest 883 problem L. Berland.Taxi(Treap+优先队列)
- CodeForces - 725D Contest Balloons 贪心
- codeforces 864 F
- codeforces contest 383 problem C(树状数组+dfs序)
- Codeforces 877 A Alex and broken contest
- codeforces - 877A - Alex and broken contest【string的一些技巧】
- codeforces contest 358
- codeforces contest 349
- Codeforces 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest B题 (排序贪心)
- 【Codeforces】2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015) E 线段树+离散化
- CodeForces - 725D Contest Balloons 贪心
- codeforces contest 779 B题
- codeforces 730G Car Repair Shop 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest