[Codeforces 864D]Make a Permutation!
Description
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 q changes.
Sample Input
4
3 2 2 3
Sample Output
2
1 2 4 3
HINT
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.
题解
又掉进了题意的坑里...
一开始以为更换数字的代价是前后数字的差值...结果发现更换代价就是$1$...水得不能再水...
那显然最小代价就是把多余的数字变成没有的数字。
对于序列的字典序,也很简单,我们玩一个小贪心,显然数字越小要放越前面。我们将没有的数字从大到小压入栈中。
从$1$~$n$遍历序列,若扫到重复的元素,如果栈顶元素比它小,显然要替换。若比它大,我们选择跳过(虽然只能跳过一次,但先跳总比后跳好),记录一下,防止之后重复跳两次。
//It is made by Awson on 2017.9.29
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = ;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
} int n, a[N+], cnt[N+];
bool skip[N+];
stack<int>S; void work() {
read(n);
for (int i = ; i <= n; i++) {
read(a[i]);
cnt[a[i]]++;
}
for (int i = n; i >= ; i--)
if (!cnt[i]) S.push(i);
printf("%d\n", S.size());
for (int i = ; i <= n; i++) {
if (cnt[a[i]] > ) {
if (skip[a[i]] || a[i] > S.top()) {
cnt[a[i]]--;
a[i] = S.top();
S.pop();
}
else skip[a[i]] = ;
}
}
for (int i = ; i <= n; i++) printf("%d ", a[i]);
}
int main() {
work();
return ;
}
[Codeforces 864D]Make a Permutation!的更多相关文章
- Codeforces 691D Swaps in Permutation
Time Limit:5000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...
- Codeforces 500B. New Year Permutation[连通性]
B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- codeforces B. Levko and Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/361/B 题目意思:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这 ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
- Codeforces 785E. Anton and Permutation
题目链接:http://codeforces.com/problemset/problem/785/E 其实可以CDQ分治... 我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多 ...
- Codeforces 804E The same permutation(构造)
[题目链接] http://codeforces.com/contest/804/problem/E [题目大意] 给出一个1到n的排列,问每两个位置都进行一次交换最终排列不变是否可能, 如果可能输出 ...
- Codeforces 785E Anton and Permutation(分块)
[题目链接] http://codeforces.com/contest/785/problem/E [题目大意] 一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数 [ ...
- Codeforces 500B New Year Permutation( Floyd + 贪心 )
B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- windows下apache报os 10048错误
在apache的bin目录下运行httpd -k install,报错os10048 (错误信息是跟443端口有关),网上的答案说的是改掉httpd.conf里的默认端口或者关闭占用端口的进程,默认端 ...
- JavaScript(第二十天)【DOM操作表格及样式】
DOM在操作生成HTML上,还是比较简明的.不过,由于浏览器总是存在兼容和陷阱,导致最终的操作就不是那么简单方便了.本章主要了解一下DOM操作表格和样式的一些知识. 一.操作表格 <table& ...
- 【iOS】Swift ?和 !(详解)
Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始值, 也就是说变量不会有默认值,所以要求使用变量之前必须要对其初始化 .如果在使用变量之前不进行初始化就会报错: [ ...
- Linux环境下发布.net core
一.安装Linux环境 1. 安装VM虚拟机和操作系统 VM虚拟工具安装的过程详见:http://blog.csdn.net/stpeace/article/details/78598333.直接按照 ...
- 调用WCF时,调用已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定。
解决方案: 其实只要在客户端配置文件中加上如下紫色粗体属性( maxReceivedMessageSize): <?xml version="1.0" encoding=&q ...
- javascript中数组的深拷贝的方法
一.什么是浅拷贝 在js当中,我们常常遇到数组复制的的情况,许多人一般都会使用"="来直接把一个数组赋值给一个变量,如 var a=[1,2,3]; var b=a; consol ...
- react-native-image-picker 运用launchCamera直接调取摄像头的缺陷及修复
在前几天用react-native进行android版本开发当中,用到了"react-native-image-picker"的插件:根据业务的需求:点击按钮-->直接调取摄 ...
- [扩展推荐] —— Laravel Log 增强
Laravel Log Enhancer 是 Laravel 5.6 的一个扩展包,可以在 Laravel 日志中添加额外的数据. 得益于 Laravel 5.6 中日志的更新,这个包利用这些特性扩 ...
- TCP/IP和HTTP协议代理
TCP/IP协议族 TCP/IP(传输控制协议/网际协议)是用于计算机通信的一个协议族. TCP/IP协议族包括诸如Internet协议(IP).地址解析协议(ARP).互联网控制信息协议(ICMP) ...
- 云计算 IaaS,SaaS,PaaS的区别?一个通俗易懂的吃货文章
来自一篇吃货文章了: ———————————————————— <img src="https://pic2.zhimg.com/a55676f8e1b084a398f8cd5 ...