Codeforce 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.
贪心
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std; int n,a[],boo[],b[],c[],tot,ans; int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
boo[a[i]]++;
}
tot=;
for(int i=;i<=n;i++)
if(!boo[i]){
tot++;
b[tot]=i;
}
b[tot+]=n+;
int k=; ans=;
for(int i=;i<=n;i++)
if(boo[a[i]]>){
if(!c[a[i]]&&b[k]>a[i]){
c[a[i]]=;
continue;
}
boo[a[i]]--;
ans++;
a[i]=b[k];
k++;
}
printf("%d\n",ans);
for(int i=;i<n;i++)
printf("%d ",a[i]);
printf("%d",a[n]);
}
Codeforce D. Make a Permutation!的更多相关文章
- codeforce 849D. Make a Permutation!
D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- codeforce 436 D贪心思维题Make a Permutation!
D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
随机推荐
- keepalived配置文件详解(2)
全局配置 global_defs { notification_email { #指定keepalived在发生切换时需要发送email到的对象,一行一个邮件地址 xuequn@.com } noti ...
- Use of Deep Learning in Modern Recommendation System: A Summary of Recent Works(笔记)
注意:论文中,很多的地方出现baseline,可以理解为参照物的意思,但是在论文中,我们还是直接将它称之为基线,也 就是对照物,参照物. 这片论文中,作者没有去做实际的实验,但是却做了一件很有意义的事 ...
- linux数据库常用指令
linux数据库环境搭建好之后,我们就可以创建数据库了,如果不是 很了解linux数据库环境搭建的话,可以到看看我上一篇文章linux数据库环境搭建 首先我们来创建一个数据库:使用指令sqlite3 ...
- Java内存管理及对Java对象管理
Java内存管理及对Java对象管理 1Java内存管理 1.1Java中的堆和栈 通常来说,人们会将Java内存氛围栈内存(Stack)和堆内存(Heap). 栈内存用来保存基本类型的变量和对象的引 ...
- 1492: [NOI2007]货币兑换Cash
Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和 B纪念券(以下 简称B券).每个持有金券的顾客都有一个自己的帐户.金券的数目可以是一个 ...
- hdu_1573 X问题(不互素的中国剩余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Me ...
- JFinal极速开发框架使用笔记(三) 分析Model和ActiveRecord
JFinal框架的一些新发现的用法: 在JFinal框架中,实体类并不需要设置属性,更不需要配置getset方法就可以很方便的操作数据库,如果需要设置或者获取属性,可以直接使用一下方式: User u ...
- 人生苦短,我用Python 第一篇
一.变量名 自定义变量名只能用 数字.字母.下划线组成. 使用变量前,需对其先赋值 注意:1.变量名不能以数字开头: 2.不能是关键字:例如:'and', 'as', 'assert', ' ...
- Personal Learning Path of Java——Java语言基础
Java语言是面向对象编程语言,Java程序的基本组成单元是类,类体中又包括属性和方法两部分.每一个程序都必须包含一个main()方法,含有main()方法的类称为主类. 如下面代码: package ...
- final、finally、finalize
final是一个修饰词.可以修饰变量.方法.类 final修饰变量时分为两种 )1.修饰成员变量:该成员变量不可以被二次赋值.也就是说成员变量无法改变.且该成员变量要么在定义时初始化,要么在构造器中进 ...