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 ...
随机推荐
- Bootstrap笔记合集
一. 为了简化操作,方便使用,Bootstrap通过定义四个类名来控制文本的对齐风格: ☑ .text-left:左对齐 ☑ .text-center:居中对齐 ☑ .text-right ...
- 都是Javascript的作用域惹得祸
案件重现 今天有位然之OA 系统的定制开发用户咨询了个问题,他想在新加的功能模块的操作面板中,实现用户点击删除按钮时提示友好提醒,如下: 问题很简单,虽然他自己最终达到目的效果了,但不知道起初问题出在 ...
- vue+axios 前端实现登录拦截(路由拦截、http拦截)
一.路由拦截 登录拦截逻辑 第一步:路由拦截 首先在定义路由的时候就需要多添加一个自定义字段requireAuth,用于判断该路由的访问是否需要登录.如果用户已经登录,则顺利进入路由, 否则就进入登录 ...
- uva 1121 Subsequence
https://vjudge.net/problem/UVA-1121 题意: 给出一个正整数数列a,要求找出最短的连续的一个序列使得这个序列的所有数字之和大于等于S. 思路: 第一是由于序列都是正整 ...
- Divide Sum 比赛时竟然想不出。。。。。。。
Divide Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSt ...
- Problem 2144 Shooting Game fzu
Problem 2144 Shooting Game Accept: 99 Submit: 465Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)ABCD
A. Arpa and a research in Mexican wave time limit per test 1 second memory limit per test 256 megaby ...
- 在Kubernetes集群中使用calico做网络驱动的配置方法
参考calico官网:http://docs.projectcalico.org/v2.0/getting-started/kubernetes/installation/hosted/kubeadm ...
- 理解js中的运算符优先级
前言 我是有过这样的经历,获取年月日时写出这样的代码: new Date().getFullYear() 此时的我是心虚的,因为我不知道是先执行.运算还是new运算,于是赶紧贴到控制台里,哎呦
- java 学习笔记 读取配置文件的三种方式
package com.itheima.servlet.cfg; import java.io.FileInputStream; import java.io.FileNotFoundExceptio ...