Codeforces 864D
来源:互联网 发布:欧楷笔法 知乎 编辑:程序博客网 时间:2024/05/17 08:25
D. Make a Permutation!
time limit per test 2seconds
memory limit per test 256megabytes
Ivan has an array consisting ofn elements. Each of the elements is aninteger from 1 to n.
Recently Ivan learned about permutations and theirlexicographical order. Now he wants to change (replace) minimum number of elements in hisarray in such a way that his array becomes a permutation (i.e. each of the integersfrom 1 to n was encountered in his array exactlyonce). If there are multiple ways to do it he wants to find thelexicographically minimalpermutation 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 islexicographically smaller, we compare their first elements. If they are equal —compare the second, and so on. If we have two permutationsx and y, then x is lexicographically smaller if xi < yi, where i is the first index in which thepermutations x and y differ.
Determine the array Ivan will obtain after performing all thechanges.
Input
The first line contains an single integern (2 ≤ n ≤ 200 000) — the number ofelements in Ivan's array.
The second line contains a sequence of integersa1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array.
Output
In the first line printq — the minimum number of elements that need to be changed inIvan's array in order to make his array a permutation. In the second line,print thelexicographicallyminimal 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 inposition1 with number one, and number two in position 3 with number four. Then he will get apermutation [1, 2, 4, 3] with only two changed numbers — this permutation islexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his arrayalready is a permutation.
【题意】
现在给出一个n,以及n个数,这n个数范围为1~n。 现在问最少改变几个数能使这n个数成为1~n的全排列,若有多种情况,使全排列的字典序最小。
【思路】
由于先要保证改变个数最小,显然在1~n中哪些数没有出现,结果就是没有出现的数的个数。
现在我们要考虑如何使字典序最小。
我们现在读入时记录每个数出现了几次。并把没有出现过的数从小到大放到队列里。
如果一个数出现了多次,显然要把它替换掉。但值得注意的是若同一个数出现有多个位置,需要保留一个位置不作变化。
若这个数没有被保留过,那么如果小于队列里最顶端的数,这个数就被保留。
但如果被保留过了,或者小于队列里最顶端的数,那么这个位置的数改为队列里最顶端的数。
这样做保证了字典序最小。
#include <cstdio>#include <cmath>#include <vector>#include <iostream>#include <set>#include <queue>#include <cstring>#include <string>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define rush() int T;scanf("%d",&T);while(T--)typedef long long ll;const int maxn = 200005;const ll mod = 1e9+7;const int INF = 0x3f3f3f3f;const double eps = 1e-9;int n,m;int a[maxn];int vis[maxn]; //某个数是否已经被填过int num[maxn];int main(){ while(~scanf("%d",&n)) { mst(vis,0); mst(num,0); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); num[a[i]]++; } queue<int>q; for(int i=1; i<=n; i++) //哪些数开始没有 { if(num[i]==0) { q.push(i); } } int ans=0; for(int i=1; i<=n; i++) { if(num[a[i]]>1) { if(vis[a[i]]==0&&a[i]<q.front()) //没填过,且比队列最顶端的数小 { vis[a[i]]=1; } else { ans++; num[a[i]]--; //特别注意 a[i]=q.front(); q.pop(); } } } printf("%d\n",ans); for(int i=1; i<=n; i++) { printf("%d ",a[i]); } puts(""); } return 0;}
- Codeforces 864D
- codeforces 864D
- codeforces 864D 模拟 + 贪心
- Codeforces 864D Round#436D :全排列乱搞
- Codeforces 864D Make a Permutation!
- CodeForces 864D Make a Permutation!
- Codeforces 864 D Make a Permutation!
- Codeforces Round #436 (Div. 2)D. Make a Permutation! codeforces-864D. Make a Permutation!
- CodeForces 617D CodeForces 617D
- CodeForces 101D
- CodeForces 103D
- CodeForces 222D Olympiad
- codeforces 242d
- CodeForces 111D
- 【codeforces】3D
- CodeForces 127D Password
- codeforces 257D. Sum
- Codeforces 292D
- 齐一都在唱什么呢+歌词词云分析
- [DP]Codeforces 743E Vladik and cards
- UI游戏框架(一)
- luogu3383 线性筛模板
- Jetty第一次访问没有首页
- Codeforces 864D
- JZOJ5385. 【NOIP2017提高A组模拟9.23】Carry
- 【BZOJ】Tyvj 1729 文艺平衡树 Splay
- hashcode是什么意思?
- S5PV210启动过程分析
- 更改Eclipse下Tomcat的部署目录
- 帅气的汇编
- [bzoj1406][数论]密码箱
- 局部敏感哈希 LSH 介绍