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.
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我们可以考虑是否修改 如果他这个值之前已经出现过并且没有修改
那么这个位置就必须修改了 不然的话 如果他的值比当前队列的值要小 那就不修改 留着之后修改这样字典序一定最小
打个标记就可以辣
#include<cstdio>
#include<cstring>
#include<algorithm>
const int M=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); 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=;i<=n;i++){k=read(); f[k]++; ans[i]=k;}
for(int i=;i<=n;i++) if(!f[i]) q[++cnt]=i;
int k=;
for(int i=;i<=n;i++){
if(f[ans[i]]>){
if(last[ans[i]]||q[k]<ans[i]) f[ans[i]]--,ans[i]=q[k++],ansh++;
else last[ans[i]]=;
}
}
printf("%d\n",ansh);
for(int i=;i<=n;i++) printf("%d ",ans[i]);
return ;
}
codeforces contest 864 problemD的更多相关文章
- codeforces——contest 864 problemE
Polycarp is in really serious trouble — his house is on fire! It's time to save the most valuable it ...
- [codeforces contest 1119 F] Niyaz and Small Degrees 解题报告 (树形DP+堆)
interlinkage: http://codeforces.com/contest/1119/problem/F description: 有一颗$n$个节点的树,每条边有一个边权 对于一个$x$ ...
- CodeForces contest/776 A+B+C题解
ICM Technex 2017 and Codeforces Round #400 (Div. 1 +Div.2,combined) A. A Serial Killer 谜一样的题意:每天从两个人 ...
- codeforces/contest/803/problem C
题目:C. Maximal GCD 题意:输入n,k.将n拆成k个数的序列,使得这k个数的gcd最大.(且序列严格递增).1 ≤ n, k ≤ 1010 . 分析:假设k个数的gcd为d,则一定有d| ...
- 【codeforces contest 1119 F】Niyaz and Small Degrees
题目 描述 \(n\) 个点的树,每条边有一个边权: 对于一个 \(X\) ,求删去一些边后使得每个点的度数 \(d_i\) 均不超过 \(X\) 的最小代价: 你需要依次输出 \(X=0 \to n ...
- CodeForces Contest #1137: Round #545 (Div. 1)
比赛传送门:CF #1137. 比赛记录:点我. 每次都自闭的 div1 啊,什么时候才能上 IM 呢. [A]Skyscrapers 题意简述: 有一个 \(n\times m\) 的矩阵 \(a_ ...
- CodeForces Contest #1114: Round #538 (Div. 2)
比赛传送门:CF #1114. 比赛记录:点我. 又 FST 了. [A]Got Any Grapes? 题意简述: 有三个人,第一个人需要吃绿色葡萄至少 \(a\) 个,第二个人需要吃绿色和紫色葡萄 ...
- CodeForces Contest #1110: Global Round 1
比赛传送门:CF #1110. 比赛记录:点我. 涨了挺多分,希望下次还能涨. [A]Parity 题意简述: 问 \(k\) 位 \(b\) 进制数 \(\overline{a_1a_2\cdots ...
- codeforces contest 1111
A. Superhero Transformation 题意: 元音和元音,辅音和辅音字母之间可以互相转换,问两个字符串是否想同: 题解:直接判断即可: #include<bits/stdc++ ...
随机推荐
- POJ 1995 (快速幂)
这道题普通做法会发生溢出且会超时,应当用快速幂来求解. 快速幂讲解 #include <cstdio> #include <cmath> using namespace std ...
- 在 visual studio 中添加 ILDASM 工具
先写下一般的用法,就是在 vs 中添加 ILDASM 工具. 添加步骤: 工具---->外部工具----->添加: 标题我一般取为 ILDASM,命令那一栏是要选择 ILDASM 的路径, ...
- OBJ文件
OBJ文件是Alias|Wavefront公司为它的一套基于工作站的3D建模和动画软件"Advanced Visualizer"开发的一种标准3D模型文件格式,很适合用于3D软件模 ...
- 在a标签的href用户#name 的可以实现页面 上下跳转
- Codeforces 1025D(区间dp)
容易想到设f[i][j][k]为i~j区间以k为根是否能构成bst.这样是O(n4)的.考虑将状态改为f[i][j][0/1]表示i~j区间以i-1/j+1为根能否构成bst.显然如果是i-1作为根的 ...
- P2613 【模板】有理数取余
题目描述 给出一个有理数 $c=\frac{a}{b}$ ,求 c mod 19260817 的值. 输入输出格式 输入格式: 一共两行. 第一行,一个整数 aa .第二行,一个整数 bb . 输出格 ...
- BZOJ1179 [Apio2009]Atm 【tarjan缩点】
1179: [Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MB Submit: 4048 Solved: 1762 [Submit][Sta ...
- python3.5安装pycrypto
在python中使用AES加密是一种有效的加密方式,如果你研究过微信公众号api就会发现,它也用的是这个加密的.在写代码的时候,要安装crypto模块,在linux或者mac上都好说,但是在windo ...
- JavaScript滚动条的制作
效果演示 这个效果的制作是借助setTimeout的第三个参数.setTimeout/setInterval,这两个函数相信前端开发同学都很熟悉.它们在非IE(6-9)浏览器中还可以如下使用: v ...
- 将shell返回的结果保存至数组
如下,我需要将u1和u2提取出保存至数组,方便后续的调用 root@ubuntu:~# lxc list+------+---------+------------------------------ ...