code forces 436 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.
/*
* 题意:给你一个序列,元素范围[1,n],有重复的,问你最少更换几个数字,使得
* 这个序列变成一个1~n的排列,并且字典序最小
*
*
* 思路: 记录一下每个元素出现的个数,有多少没出现的,就要更换多少,然后从
* 头开始替换,有限替换成字典序小的字符,如果要替换的字符,还要小,就加个
* 标记,先不替换,最后再替换,这种策略保证了字典序最小
*
*
* */
#include <bits/stdc++.h> #define MAXN 200005
using namespace std; int n;
int a[MAXN];
int vis[MAXN];//记录每个数字出现的次数
bool is[MAXN];
int cnt;
int pos;
int num; inline void init(){
memset(vis,,sizeof vis);
memset(is,false,sizeof is);
pos=;
cnt=;
} int main(){
init();
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&a[i]);
vis[a[i]]++;
}
for(int i=;i<=n;i++)
if(vis[i]==)
cnt++;
printf("%d\n",cnt);
for(int i=;i<n;i++){
if(vis[a[i]]>){
while(vis[pos]!=)
pos++;
if(a[i]>pos){
vis[a[i]]--;
a[i]=pos;
pos++;
}else{
if(is[a[i]]==true){
vis[a[i]]--;
a[i]=pos;
pos++;
}else{
is[a[i]]=true;
}
}
}
}
for(int i=;i<n;i++){
printf(i?" %d":"%d",a[i]);
}
puts("");
return ;
}
code forces 436 D. Make a Permutation!的更多相关文章
- code forces 436 C. Bus
C. Bus time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
- Code Forces 652C Foe Pairs
C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)
Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- code forces Watermelon
/* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...
随机推荐
- NSTimer、CADisplayLink 内存泄漏
NSTimer.CADisplayLink 内存泄漏 内存泄漏的原因 CADisplayLink 要用 Taget 和 Selector 初始化,NSTimer 也可以用类似的方法初始化.这样初始化之 ...
- SpringMVC——使用RequestDispatcher.include()和HttpServletResponseWrapper动态获取jsp输出内容
介绍本篇内容前,先抛出我遇到的问题或者说是需求!(精读阅读本篇可能花费您15分钟,略读需5分钟左右) 一:需求说明 有一个Controller有两个方法 第一个方法通过指定的路径和参数去渲染jsp内容 ...
- java一些问题的思考
1.思考 为什么java规定作为程序入口点的main() 方法静态的? 在java中,main()方法是java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这 ...
- java数据库编程之事务、视图、索引、备份、恢复
第五章:事务.视图.索引.备份和恢复 5.1:事务 事务的概念:事务(transcation)是讲一系列数据操作捆绑成为一个整体进行统计管理. 如果某一事务执行成功了,则该事务进行操作的所有数据将会提 ...
- C#中System.DateTime.Now.ToString()用法
//Asp.net中的日期处理函数 //2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 ...
- windows Tomcat+Nginx 集群 迷你版
一. 准备 两个Tomcat 加上Nginx 2. 创建一个公共的文件夹用于部署项目 3. Tomcat配置 配置内存 在catalina.bat 第一行增加 set JAVA_OPTS=-Xms51 ...
- SqlServer和Oracle中一些常用的sql语句5 流程控制语句
--在sql语句中 begin...end 用来设定一个程序块 相关于c#中的{} declare @yz real,@w int --声明变量 set @w=120 --为变量赋值 if @w< ...
- Python和SQL Server 2017的强大功能
Python和SQL Server 2017的强大功能 摘要: 源:https://www.red-gate.com/simple-talk/sql/sql-development/power-pyt ...
- Scala基础之注解(annotation
在学习Scala的过程中,总会碰到一些注解: // Predef.scala @inline def implicitly[T](implicit e: T) = e @deprecated(&quo ...
- 史上最难的一道Java面试题 (分析篇)
博客园 匠心零度 转载请注明原创出处,谢谢! 无意中了解到如下题目,觉得蛮好. 题目如下: public class TestSync2 implements Runnable { int b = 1 ...