D. Make a Permutation!(思维)
2 seconds
256 megabytes
standard input
standard output
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.
4
3 2 2 3
2
1 2 4 3
6
4 5 6 3 2 1
0
4 5 6 3 2 1
10
6 8 4 6 7 1 6 3 4 5
3
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 ~ n中那些你没有用过的元素,问你需要更改多少次,以及最小的元素序列是什么?
思路:首先,我先将那些多余的重复元素和那些没有用过的元素记录下来,然后就进行遍历判断。假如当前元素时重复元素,并且当前元素比未使用过的最小元素大的话,就将其覆盖,否则,你就需要记录当前元素已经使用,当之后在遇到这个元素的时候,我就可以直接覆盖。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map> using namespace std; #define INF 0x3f3f3f3f
typedef long long ll; const int maxn = 2e5+; int vis[maxn];
int v[maxn];
int n;
int arr[maxn];
int b[maxn]; int main() {
scanf("%d", &n);
int k = ;
for(int i = ; i <= n; i++) {
scanf("%d", &arr[i]);
vis[arr[i]]++; //记录使用过的每个元素的个数
}
for(int i = ; i <= n; i++) {
if(!vis[i]) {
b[k++] = i; //记录那些没有使用的元素
}
}
int j = ;
for(int i = ; i <= n; i++) {
int x = arr[i];
if(vis[x] > ) { //当使用过的元素有多个时
if(b[j] < x) { //如果当前未使用的最小元素比其小,那么直接覆盖
arr[i] = b[j++];
vis[x]--;
} else {
if(v[x]) { //当前元素已被记录,在它前面有个和它一样的
arr[i] = b[j++];
}
v[x]++;
} }
}
printf("%d\n", k);
for(int i = ; i <= n; i++) {
printf("%d%c", arr[i], " \n"[i == n]);
}
return ;
}
D. Make a Permutation!(思维)的更多相关文章
- MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值
F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...
- leetcode 484. Find Permutation 思维题
https://leetcode.com/contest/leetcode-weekly-contest-16a/problems/find-permutation/ 设原本的数字是0,那么按照它的D ...
- Codeforces 102394I Interesting Permutation 思维
题意: 你有一个长度为n的序列a(这个序列只能使用[1,n]区间内的数字,每个数字只能使用一次),通过a序列可以构造出来三个相同长度的序列f.g.h For each 1≤i≤n, fi=max{a1 ...
- permutation 2(递推 + 思维)
permutation 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)
[Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...
- TOJ 2130: Permutation Recovery(思维+vector的使用)
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2130 时间限制(普通/Java): ...
- Permutation(构造+思维)
A permutation p is an ordered group of numbers p1, p2, ..., pn, consisting of ndistinct positi ...
- hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs
题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...
- codeforce 436 D贪心思维题Make a Permutation!
D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- maraidb忘记数据密码
一.概述 服务器上安装了maraidb 数据库,但是很久未使用过它,需要使用时,忘记了密码, 此时可以给它重新设置密码. 二.操作 修改密码 修改 /etc/my.cnf,修改下图红色区域位置,修改成 ...
- ccpc湘潭邀请赛 Partial Sum
选定最多m的区间,使区间和的绝对值最大.但是左右端点不能重复选取 首先涉及到区间和的问题,就应该想到用前缀和去优化 这里对前缀和排序 然后贪心的去选取最大.次大 (比赛的时候脑子堵的很,没想出来 可惜 ...
- python操作MySQL数据库(转)
先来一个简单的例子吧: ? 1 2 3 4 5 6 7 8 9 10 import MySQLdb try: conn=MySQLdb.connect(host='localhost',u ...
- O032、Nova reboot 和 lock 操作
参考https://www.cnblogs.com/CloudMan6/p/5479408.html 前面通过日志详细分析了 nova 的 launch.shutoff .start 操作. ...
- JS的 delete操作符 删除对象属性
JS如何删除对象中的某一属性 var obj={ name: 'zhagnsan', age: 19 } delete obj.name //true typeof obj.name //undefi ...
- eclipse 导入外面的jar
Eclipse中导入外部jar包 听语音 | 浏览:52620 | 更新:2014-12-07 20:59 | 标签:eclipse 1 2 3 4 5 6 7 分步阅读 在编写java代码时,为方便 ...
- maven入门-- part5 本地仓库,远程仓库,私服
解读Maven在仓库中的存储路径: 1.基于groupId准备路径,将句点分隔符转成路径分隔符,就是将 "." 转换成 "/" ; example: org ...
- Delphi DLL文件的静态调用
- C库函数——字符串转数字整理
atof(将字符串转换成浮点型数)atoi(将字符串转换成整型数)atol(将字符串转换成长整型数)strtod(将字符串转换成浮点数)strtol(将字符串转换成长整型数)strtoul(将字符串转 ...
- SQL语句复习【专题八】
SQL语句复习[专题八] 序列 Sequence.数据库对象是 oracle 专有的.作用:可以将某一列的值使用序列,来实现自动增长的功能.访问序列的值.[序列有两个属性 nextval currva ...