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++ ...
随机推荐
- 基于TensorFlow解决手写数字识别的Softmax方法、多层卷积网络方法和前馈神经网络方法
一.基于TensorFlow的softmax回归模型解决手写字母识别问题 详细步骤如下: 1.加载MNIST数据: input_data.read_data_sets('MNIST_data',one ...
- LintCode-66.二叉树的前序遍历
二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,2,3]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 非递归 c ...
- Spring Boot(二)配置分析
回顾一下采用SSM开发项目时,项目中会存在多个配置文件,比如web.xml,配置Spring相关的applicationContext-springmvc.xml, applicationContex ...
- 判断两个字符串是否相等【JAVA】
if(A.equals(B)){ } 之前总是用"=="来判断,但是在JAVA里面好像不行.所以,用equals(). 查了下资料. 原因:equal()比较的是对象的内容,&qu ...
- Delphi XE4 TStringHelper用法详解
原文地址:Delphi XE4 TStringHelper用法详解作者:天下为公 Delphi XE4的TStringHelper,对操作字符串进一步带来更多的方法,估计XE5还能继续用到. Syst ...
- 窗口中各模块的切换效果,使用jquery实现
用到了两个js库,请自行下载,用到的背景图片可任意图片都可以,主要是看效果 <!DOCTYPE html> <html> <head> <script src ...
- BZOJ 2120 数颜色(树状数组套主席树)
1A啊,激动. 首先,不修改的情况下可以直接用主席树搞,修改的话,直接用主席树搞一次修改的情况下复杂度是O(nlogn)的. 就像你要求区间和一样,用前缀和查询是O(1),修改是O(n),只不过主席树 ...
- BZOJ 1415 聪聪和可可(期望DP)
我们可以用n次BFS预处理出 to[][]数组,to[i][j]表示聪聪从i点到j点第一步会走哪个点. 那么对于聪聪在i点,可可在j点,聪聪先走,定义dp[i][j]表示步数期望. 那么显然有dp[i ...
- BZOJ 1046 上升序列(LIS变形)
要保证长度为L的序列下标字典序最小,当然要尽量选前面的数. 如何判断前面的数是否满足条件?,只需要知道这个数开头的递增序列的最长长度是多少,如果不小于L,那么必然可以加入这个数.还需判断一下它是否大于 ...
- 优先队列实现 大小根堆 解决top k 问题
摘于:http://my.oschina.net/leejun2005/blog/135085 目录:[ - ] 1.认识 PriorityQueue 2.应用:求 Top K 大/小 的元素 3 ...