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)!+ ...
随机推荐
- Mac从零配置Vim
// 这是一篇导入进来的旧博客,可能有时效性问题. 1. 安装Homebrew (包管理器,用来安装Vim)& /usr/bin/ruby -e "$(curl -fsSL http ...
- BC#65T4 ZYB's Tree
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5593 点分治被卡了TAT... 正解是dp,可以按层数考虑dp,先预处理跑一边dfs得到子树各层数点数大小 ...
- hdu_1754I Hate It(线段树)
hdu_1754I Hate It(线段树) 标签: 线段树 题目链接 题意: 中文题意...不多说了,线段树基础题 直接上代码: #include<cstdio> #include< ...
- MySQL数据库全备
#function:MYSQL自动全备 #version:1.0.0 #author:wangyanlin #date:2017/08/03 #---------------------------- ...
- Linux编译安装Mariadb数据库
一.安装cmake cd /usr/local/src tar zxvf cmake-2.8.12.1.tar.gz cd cmake-2.8.12.1 ./configure 注意报错需要安装gcc ...
- flume1.8 Sources类型介绍(二)
1 Flume Sources 1.1 Avro Source 监听Avro端口,从Avro client streams接收events.要求属性是粗体字. agent a1例子: ipFilter ...
- java构建学生管理系统(一)
用java搭建学生管理系统,重要还是对数据库的操作,诸如增删改查等. 1.基本的功能: 老师完成对学生信息的查看和修改,完成对班级的信息的概览. 学生可以看自己的成绩和对自己信息的修改. 学生和老师有 ...
- 安装配置最强Spark IDE--IDEA
1.安装IntelliJ IDEA IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示 ...
- 在虚拟机(VMware)中安装Linux CentOS 6.4系统(图解) 转
一.下载最新版本Linux CentOS 1.打开官网地址:http://www.centos.org/,点击Downloads->Mirrors 2.点击CentOS ...
- 【转载备忘】PowerDesigner16.5基本使用
这两天都在设计数据库,使用了powerdesigner进行设计的,然后摸索了好久,本来打算写一篇文章来记述一下的,写了一半,突然发现网上早就有比我写的好的文章了,所有删了之前写的,直接贴出来那个文章的 ...