【Codeforces - 864D】Make a Permutation!(贪心,字典序)

题干:

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.

Input

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.

Output

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 qchanges.

Examples

Input

4
3 2 2 3

Output

2
1 2 4 3 

Input

6
4 5 6 3 2 1

Output

0
4 5 6 3 2 1 

Input

10
6 8 4 6 7 1 6 3 4 5

Output

3
2 8 4 6 7 1 9 3 10 5 

Note

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的数的序列,这n个数范围为1~n。 现在问最少改变几个数能使这n个数成为1~n的某个全排列,若有多种情况,要求使全排列的字典序最小。输出改变的数的个数和这个全排列。

解题报告:

涉及字典序最小的问题其实比较好构造,就是优先贪心前面的,因为前面的变小了要优秀于后面无论多花里胡哨的操作。

首先需要改变的数很好求出,然后对于输出全排列,就先把需要添加的数求出来,排个序,然后从前到后对原序列贪心搞一搞就好了。vis数组考虑这个数是否出现过。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,a[MAX],cnt[MAX],res[MAX],tot,ans,vis[MAX];
int main()
{cin>>n;for(int i = 1; i<=n; i++) scanf("%d",a+i),cnt[a[i]]++;for(int i = 1; i<=n; i++) {if(cnt[i] == 0) res[++tot] = i;if(cnt[i] > 1) ans += cnt[i]-1;}sort(res+1,res+tot+1);int cur = 1;for(int i = 1; i<=n; i++) {if(cnt[a[i]] == 1) continue;if(vis[a[i]]) cnt[a[i]]--,a[i] = res[cur++];else {if(res[cur] < a[i]) cnt[a[i]]--,a[i] = res[cur++];else vis[a[i]]=1;}}printf("%d\n",ans);for(int i = 1; i<=n; i++) printf("%d ",a[i]);return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439693.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

百度顶会论文复现(3):视频分类综述

本节课主要是对视频分类的发展进行了介绍&#xff0c;包括任务与背景&#xff0c;分类方法&#xff0c;前沿进展等。课程地址为&#xff1a;https://aistudio.baidu.com/aistudio/course/introduce/1340?directly1&shared1。 文章目录1. 任务与背景2. 视频分类方法2.1 双流…

一步步编写操作系统 46 linux的elf可执行文件格式1

ELF文件格式依然是分为文件头和文件体两部分&#xff0c;只是该文件头相对稍显复杂&#xff0c;类似层次化结构&#xff0c;先用个ELF header从“全局上”给出程序文件的组织结构&#xff0c;概要出程序中其它头表的位置大小等信息&#xff0c;如程序头表的大小及位置、节头表的…

百度顶会论文复现(4):飞桨API详解

本节课主要是对飞桨常用API进行了介绍&#xff0c;课程地址为&#xff1a;https://aistudio.baidu.com/aistudio/education/group/info/1340。 文章目录1.飞桨API官网2. API使用介绍3. 飞桨模型操作1.飞桨API官网 官网地址为&#xff1a;https://www.paddlepaddle.org.cn/docu…

【Codeforces - 977D】Divide by three, multiply by two(思维构造)

题干&#xff1a; Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, and then performs with it n−1n−1 operations of the two kinds: divide the number xx by 33 (xx must be divisible by 33);multiply the numbe…

一步步编写操作系统 45 linux的elf可执行文件中的段和节

接上文&#xff0c;为了描述清楚文件格式的本质&#xff0c;咱们先从最基本的“段”说起。 程序中最重要的部分就是段&#xff08;segment&#xff09;和节&#xff08;section&#xff09;&#xff0c;它们是真正的程序体&#xff0c;是真真切切的程序资源&#xff0c;所以下…

视觉SLAM十四讲(3):三维空间刚体运动

本章需要掌握的知识点有&#xff1a;旋转矩阵&#xff0c;变换矩阵&#xff0c;四元数&#xff0c;欧拉角定义和数学表达&#xff1b;同时也要掌握Eigen库关于矩阵、几何模块的使用方法。 文章目录3.1 旋转矩阵3.1.1 点&#xff0c;向量和矩阵的关系3.1.2 坐标系间的欧式变换3.…

【CodeForces - 483C】Diverse Permutation(思维构造)

题干&#xff1a; Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of ndistinct positive integers not larger than n. Well denote as n the length of permutation p1,   p2,   ...,   pn. Your task is to find such…

一步步编写操作系统 47 elf格式文件分析实验

在上一节中&#xff0c;我们讲述了elf格式的部分理论知识&#xff0c;为什么是部分呢&#xff1f;因为我们本着“够用”的原则&#xff0c;只把我们需要了解的部分说完啦。不过&#xff0c;我相信大部分同学仅仅凭上一节中的理论知识还是领悟不到elf本质&#xff0c;咱们在本节…

百度飞桨顶会论文复现(5):视频分类论文之《Representation Flow for Action Recognition》篇

这次老师在课上总共领读了4篇分类论文&#xff0c;我这里分享其中的一篇论文&#xff0c;是关于使用神经网络对光流进行学习。 课程地址是&#xff1a;https://aistudio.baidu.com/aistudio/education/group/info/1340。 论文地址是&#xff1a;https://arxiv.org/abs/1810.014…

智能算法(GA、DBO等)求解零等待流水车间调度问题(NWFSP)

先做一个声明&#xff1a;文章是由我的个人公众号中的推送直接复制粘贴而来&#xff0c;因此对智能优化算法感兴趣的朋友&#xff0c;可关注我的个人公众号&#xff1a;启发式算法讨论。我会不定期在公众号里分享不同的智能优化算法&#xff0c;经典的&#xff0c;或者是近几年…

【蓝桥官网试题 - 算法提高】change(思维)

题干&#xff1a; 问题描述 数组A中共有n个元素&#xff0c;初始全为0。你可以对数组进行两种操作&#xff1a;1、将数组中的一个元素加1&#xff1b;2、将数组中所有元素乘2。求将数组A从初始状态变为目标状态B所需要的最少操作数。 输入格式 第一行一个正整数n表示数组中元…

一步步编写操作系统 50 加载内核3

接上节&#xff0c;在这里&#xff0c;我们把参数放到了栈中保存&#xff0c;大家注意到了&#xff0c;参数入栈的顺序是先从最右边的开始&#xff0c;最后压入的参数最左边的&#xff0c;其实这是某种约定&#xff0c;要不&#xff0c;为什么不先把中间的参数src入栈呢。既然主…

【POJ - 2965】The Pilots Brothers' refrigerator(暴力枚举,思维)

题干&#xff1a; The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator. There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrige…

动手学无人驾驶(5):多传感器数据融合

本系列的前4篇文章主要介绍了深度学习技术在无人驾驶环境感知中的应用&#xff0c;包括交通标志识别&#xff0c;图像与点云3D目标检测。关于环境感知部分的介绍本系列暂且告一段落&#xff0c;后续如有需要再进行补充。 现在我们开启新的篇章&#xff0c;在本文中将会介绍无人…

一步步编写操作系统 52 深入浅出cpu的特权级

所谓保护模式下的“保护”&#xff0c;主要体现在特权级上&#xff0c;以后随着后面工作的展开&#xff0c;会越来越多的和它们打交道&#xff0c;现在是时候说道说道了。 在人类社会中出现恶势力时&#xff0c;人们总是希望出现一位具有神力的英雄来拯救世人、主持公道。阶级…

详解两阶段3D目标检测网络PVRCNN:Point-Voxel Feature Set Abstraction for 3D Object Detection

在《动手学无人驾驶&#xff08;4&#xff09;&#xff1a;基于激光雷达点云数据3D目标检测》一文中介绍了3D目标检测网络PointRCNN。今天介绍该作者新提出的3D检测模型&#xff1a;PVRCNN&#xff0c;论文已收录于CVPR2020。 作者个人主页为&#xff1a;https://sshaoshuai.gi…

【Codeforces - 755C】PolandBall and Forest(并查集)

题干&#xff1a; PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs with k vertices and k - 1 edges, where k is some integer. Note that one vertex is a valid tree. There is exactly one …

一步步编写操作系统 53 任务状态段TSS介绍

操作系统是利用PCB来维护所有任务的&#xff0c;包括进程和线程&#xff0c;但cpu提供的是TSS&#xff0c;linux系统可没用它&#xff0c;因为效率太低。但是还是要了解下TSS才清楚操作系统中某些操作的原因。 本节中所讲的特权级与它有着密不可分的联系&#xff0c;TSS作用不…

动手学无人驾驶(6):基于IMU和GPS数据融合的自车定位

在上一篇博文《动手学无人驾驶&#xff08;5&#xff09;&#xff1a;多传感器数据融合》介绍了如何使用Radar和LiDAR数据对自行车进行追踪&#xff0c;这是对汽车外界运动物体进行定位。 对于自动驾驶的汽车来说&#xff0c;有时也需要对自身进行更精确的定位&#xff0c;今天…

【Codeforces - 798C】 Mike and gcd problem(思维,贪心)

题干&#xff1a; Mike has a sequence A  [a1, a2, ..., an] of length n. He considers the sequence B  [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beauti…