D. Make a Permutation! (Codeforces Round #436 (Div. 2))
来源:互联网 发布:室内水电设计软件 编辑:程序博客网 时间:2024/06/07 02:59
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.
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.
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.
43 2 2 3
21 2 4 3
64 5 6 3 2 1
04 5 6 3 2 1
106 8 4 6 7 1 6 3 4 5
32 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.
把给的数的集合,变成1~n的排列,必须是最小的排列
#include <unordered_map>#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>using namespace std;const int maxn = 200005;int num[maxn];int flag[maxn];int a[maxn];int unused[maxn];int main() { //freopen("in.txt", "r", stdin); std::ios::sync_with_stdio(false); std::cin.tie(0); memset(num, 0, sizeof(num)); memset(flag, 0, sizeof(flag)); memset(unused, 0, sizeof(unused)); int n, ans = 0; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; if (num[a[i]]) ans++; num[a[i]]++; } for (int i = 1; i <= n; ++i) { unused[i] = !num[i]; } for (int i = 0,j = 1; i < n; ++i) { if (num[a[i]] > 1) { while (!unused[j]) ++j; if (a[i] > j || flag[a[i]]) { --num[a[i]]; a[i] = j++; } else { flag[a[i]] = 1; } } } cout << ans << endl; for (int i = 0; i < n; ++i) { cout << (i == 0 ? "" : " ") << a[i]; } cout << endl;}
- 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
- Python爬虫学习:我的第一个爬虫—爬取贴吧感兴趣帖子的图片(大家都懂,哈哈)
- 目前为止最全的微信小程序项目实例
- log4j配置
- Linux系统安装Oracle11g完整安装图文教程
- python 爬虫
- D. Make a Permutation! (Codeforces Round #436 (Div. 2))
- 结构体、字符串函数复习记录
- eclipse A .project file already exists in directory
- 设计模式之策略模式Strategy
- PHP,Mysql-根据一个给定经纬度的点,进行附近地点查询–合理利用算法,效率提高2125倍
- systemd (简体中文)
- 运维不得不知的 Linux 性能监控、测试、优化工具
- MyEclipse如何去掉对JS等文件的验证
- python 爬虫