codeforce864d
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.
输入n 输入n个数字 这n个数字可能会有重复的 让你进行替换 结果要变成由1-n排列的n个数字
思路:贪心 记录一下每个数字出现的地方,我选择queue<int> arr[2e5+5]
queue<int,vector<int>,greater<int> > pp; 装没有出现过的数字
首先输入n个数字for(i = 1; i <= n; ++i) cin >> ans[i];
for(i = 1; i <= n; ++i) 当arr[ans[i]].size() == 1那就跳过 如果大于1
否则就判断一下 pp.top()是否大于当前的ans[i],小于就替换
大于等于就不替换 下次再扫描到相同的数值时再替换
#include <iostream>
#include <queue>
#include <string>
#include <cstring>
using namespace std;
const int maxn = 2e5+5;
queue<int> ans[maxn];
int main()
{
int n,i,j,m,sum = 0;
priority_queue<int,vector<int>,greater<int> > pp;
int arr[maxn];
bool pan[maxn];
memset(pan, 0, sizeof(pan));
scanf("%d",&n);
for(i = 1; i <= n; ++i)
{
scanf("%d",arr+i);
ans[arr[i]].push(i);
}
for(i = 1; i <= n; ++i)
{
if(ans[i].size() == 0)
pp.push(i);
if(ans[i].size() > 1)
sum += ans[i].size() - 1;
}
for(i = 1; i <= n; ++i)
{
if(ans[arr[i]].size() == 1)
continue;
if(pp.top() < arr[i] || pan[arr[i]])
{
ans[arr[i]].pop();
arr[i] = pp.top();
pp.pop();
continue;
}
pan[arr[i]] = true;
}
cout << sum << endl;
for(i = 1; i < n; ++i)
cout << arr[i] << " ";
cout << arr[i] << endl;
}
codeforce864d的更多相关文章
随机推荐
- Linux惊群效应详解
Linux惊群效应详解(最详细的了吧) linux惊群效应 详细的介绍什么是惊群,惊群在线程和进程中的具体表现,惊群的系统消耗和惊群的处理方法. 1.惊群效应是什么? 惊群效应也有人 ...
- 基于Web Service的客户端框架搭建一:C#使用Http Post方式传递Json数据字符串调用Web Service
引言 前段时间一直在做一个ERP系统,随着系统功能的完善,客户端(CS模式)变得越来越臃肿.现在想将业务逻辑层以下部分和界面层分离,使用Web Service来做.由于C#中通过直接添加引用的方来调用 ...
- python入门之深浅copy
a1=["a","b","c","aa"] b1=a1 a1[0]=" print(a1,b1) 此时结果为: ...
- 求先序排列(NOIP2001&NOIP水题测试(2017082301))
题目链接:求先序排列 这道题讲白了,就是数的构造,然后遍历. 思路大致是这样: 我们先通过后序遍历,找到当前区间的根,然后在中序遍历中找到根对应的下标,然后就可以分出左右子树,建立当前根与左右子树根的 ...
- kbmMW均衡负载与容灾(3)(转载红鱼儿)
在kbmMW均衡负载与容灾(1)中,介绍了利用ClientTransport的OnReconnect事件,对联接的应用服务器的地址进行更换,做容灾处理.实际上,作者还给我们提供了另外一种机制,直接在C ...
- ubuntu禁用n卡驱动(进系统卡死)
显卡驱动 该发行版依旧内置了Nouveau 开源驱动,这是导致频繁死机的直接原因.接下来要做的三件事情是: 禁用Nouveau 内核模块 安装Intel HD 530 驱动(二选一) 安装NVIDIA ...
- 建库,建表,添加数据 SQL命令
create database ssm default character set utf8; use ssm; create table flower( id int(10) primary key ...
- 2018.12.31 bzoj3771: Triple(生成函数+fft+容斥原理)
传送门 生成函数经典题. 题意简述:给出nnn个数,可以从中选1/2/31/2/31/2/3个,问所有可能的和对应的方案数. 思路: 令A(x),B(x),C(x)A(x),B(x),C(x)A(x) ...
- 2018.11.07 NOIP训练 lzy的游戏(01背包)
传送门 考虑对于每次最后全部选完之后剩下的牌的集合都对应着一种构造方法. 一个更接地气的说法: 设消耗的牌数为ttt,如果使用的牌的lll值之和也为ttt,则对应着一种构造方式让这种情形成立. 于是做 ...
- 2018.11.03 NOIP模拟 图(bfs/最短路)
传送门 显然如果AAA到BBB或者CCC到DDD走的不是最短路一定是有一段路径重合了,于是可以O(n2)bfsO(n^2)bfsO(n2)bfs出两点之间的最短距离然后枚举两个点作为重合的端点来更新答 ...