D. Make a Permutation!
time limit per test:

2 seconds

memory limit per test:

256 megabytes

input:

standard input

output:

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.

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.

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.

题目链接:http://codeforces.com/contest/864/problem/D

题意:改变最少的数,使得1-n每个数均只出现一次,并且字典序最小。

思路:依次从小到大确定数字可以出现的最前位置即可。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define bug(x) cout<<"bug"<<x<<endl;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int > P;
const int N=3e5+;
int a[N];
bool vis[N];
queue<int>poi[N];
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
poi[a[i]].push(i);
}
for(int i=; i<=n; i++)
if(poi[i].size()>) q.push(poi[i].front());
memset(vis,false,sizeof(vis));
int x,ans=;
for(int i=; i<=n; i++)
{
if(poi[i].size()>=)
{
x=poi[i].front();
vis[x]=true;
}
else
{
while(vis[q.top()]) q.pop();
x=q.top(),q.pop();
ans++;
}
poi[a[x]].pop();
if(a[x]<=i&&poi[a[x]].size()) q.push(poi[a[x]].front());
else if(a[x]>i&&poi[a[x]].size()>) q.push(poi[a[x]].front());
a[x]=i;
}
printf("%d\n",ans);
for(int i=; i<n; i++) cout<<a[i]<<" ";
printf("%d\n",a[n]);
return ;
}

Codeforces Round #436 (Div. 2)D. Make a Permutation! 模拟的更多相关文章

  1. Codeforces Round #436 (Div. 2) D. Make a Permutation!

    http://codeforces.com/contest/864/problem/D 题意: 给出n和n个数(ai <= n),要求改变其中某些数,使得这n个数为1到n的一个排列,首先保证修改 ...

  2. 【贪心】Codeforces Round #436 (Div. 2) D. Make a Permutation!

    题意:给你一个长度为n的数组,每个元素都在1~n之间,要你改变最少的元素,使得它变成一个1~n的排列.在保证改动最少的基础上,要求字典序最小. 预处理cnt数组,cnt[i]代表i在原序列中出现的次数 ...

  3. Codeforces Round #436 (Div. 2)【A、B、C、D、E】

    Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张 ...

  4. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  5. Codeforces Round #436 (Div. 2) C. Bus

    http://codeforces.com/contest/864/problem/C 题意: 坐标轴上有x = 0和 x = a两点,汽车从0到a之后掉头返回,从a到0之后又掉头驶向a...从0到a ...

  6. Codeforces Round #436 (Div. 2) E. Fire

    http://codeforces.com/contest/864/problem/E 题意: 有一堆物品,每个物品有3个属性,需要的时间,失效的时间(一开始)和价值.只能一件一件的选择物品(即在选择 ...

  7. Codeforces Round #436 (Div. 2)

    http://codeforces.com/contest/864 第一次打cf的月赛-- A 题意:给你一个数列,问你能不能保证里面只有两种数且个数相等.2<=n<=100,1<= ...

  8. Codeforces Round #436 (Div. 2) B. Polycarp and Letters

    http://codeforces.com/contest/864/problem/B 题意: 给出一个字符串,要求找到一个集合S,使得从S中选出的所有数,在这些数的位置上的字母全部为小写且是不同的字 ...

  9. Codeforces Round #436 (Div. 2)C. Bus 模拟

    C. Bus time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input out ...

随机推荐

  1. HTTP 请求头中的 Remote_Addr,X-Forwarded-For,X-Real-IP

    REMOTE_ADDR 表示发出请求的远程主机的 IP 地址,remote_addr代表客户端的IP,但它的值不是由客户端提供的,而是服务端根据客户端的ip指定的,当你的浏览器访问某个网站时,假设中间 ...

  2. js实现选中div内容并复制到剪切板

    function copyUrl () { var div = document.getElementById('xxxx'); if (document.body.createTextRange) ...

  3. 20164304姜奥——Exp1 PC平台逆向破解

    1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,ge ...

  4. ie8兼容性总结

    DOCTYPE 首先需要确保你的HTML页面开始部分要有DOCTYPE声明.DOCTYPE告诉浏览器使用什么样的HTML或XHTML规范来解析HTML文档,具体会影响: 对标记.attributes ...

  5. Java数据类型(primitive)原始数据类型

    1.小心别溢出来. 要确保变量能存下来所保存的值. 你无法用小杯子装大值.好吧,其实可以,但是会损失某些信息,也就是所说的溢位.当判断到所使用的容器不足以装载时,编译器会试着防止珍重情况发生.举例来说 ...

  6. 【Debug】逻辑分析仪数据错乱,看波形为信号耦合导致数据错乱,实际上为逻辑分析仪地线没接上!

    如图都有数据的时间段,数据错乱,实际为逻辑分析仪地线未接,接上就不会了.

  7. Create Oracle database using dbca in silent mode

    12.2.0.1 dbca.rsp文件中必须内容如下: responseFileVersion=/oracle/assistants/rspfmt_dbca_response_schema_v12.2 ...

  8. 基于WebGL架构的3D可视化平台—新风系统演示

    新风系统是根据在密闭的室内一侧用专用设备向室内送新风,再从另一侧由专用设备向室外排出,在室内会形成“新风流动场”,从而满足室内新风换气的需要.实施方案是:采用高风压.大流量风机.依靠机械强力由一侧向室 ...

  9. Go 指针

    变量是存储值得地方. 借助声明变量时使用的名字来区分. 指针的值是一个变量的地址.一个指针指示值所保存的位置.不是所有的值都有地址,但是所有的变量都有.使用指针,可以在无需知道变量名字的情况下,间接读 ...

  10. Spring MVC随笔记录

    根据https://blog.csdn.net/abc997995674/article/details/80353410整理 @ModelAttribute 可以用在方法.方法参数上,也可以和@re ...