Codeforces Round #436 D. Make a Permutation!
题意:给你n个数字,其中可能有相同的数字,要求你用其他的数字替换这些相同的数字,使得所得的序列字典序最小。
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 思路:暴力就好,那数组记录一下,注意:替换的时候要用比他小的换,如果选择了一个不换,那么后面的数就必须要换掉了。 代码:
#include<iostream>
#include<string.h>
using namespace std;
const int maxn=2e5+5;
int num[maxn],a[maxn];
bool f[maxn];
//f标记数组,标记有没有出现没有换的情况
int main(){
int n;
cin>>n;
memset(num,0,sizeof(num));
memset(f,0,sizeof(f));
memset(a,0,sizeof(a));
for(int i=0;i<n;i++){
cin>>a[i];
num[a[i]]++; //num记录每个数出现了几次
}
int cur=1,sum=0; //cur表示用来替换的数
for(int i=0;i<n;i++){
if(num[a[i]]>1){
while(num[cur])cur++; //要找一个没有出现的数来换
if(cur<a[i]||f[a[i]]==1){ //如果cur小于当前的数就换;或者之前有一个没有换,那么之后的都必须换了
num[a[i]]--;
a[i]=cur;
num[cur]=1;
sum++;
}
else f[a[i]]=1; //如果cur大于当前这个数,可以选择不换,就标记一下。
}
}
cout<<sum<<endl;
for(int i=0;i<n;i++){
if(i!=0)cout<<' ';
cout<<a[i];
}
return 0;
}
Codeforces Round #436 D. Make a Permutation!的更多相关文章
- Codeforces Round #436 (Div. 2)【A、B、C、D、E】
Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张 ...
- Codeforces Round #436 (Div. 2)D. Make a Permutation! 模拟
D. Make a Permutation! time limit per test: 2 seconds memory limit per test: 256 megabytes input: st ...
- Codeforces Round #436 (Div. 2) D. Make a Permutation!
http://codeforces.com/contest/864/problem/D 题意: 给出n和n个数(ai <= n),要求改变其中某些数,使得这n个数为1到n的一个排列,首先保证修改 ...
- 【贪心】Codeforces Round #436 (Div. 2) D. Make a Permutation!
题意:给你一个长度为n的数组,每个元素都在1~n之间,要你改变最少的元素,使得它变成一个1~n的排列.在保证改动最少的基础上,要求字典序最小. 预处理cnt数组,cnt[i]代表i在原序列中出现的次数 ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
- Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)
题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...
- 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 ...
- Codeforces Round #436 (Div. 2) E. Fire
http://codeforces.com/contest/864/problem/E 题意: 有一堆物品,每个物品有3个属性,需要的时间,失效的时间(一开始)和价值.只能一件一件的选择物品(即在选择 ...
随机推荐
- php使用select语句查询数据信息
<html> <head> <title>Finding User</title> </head> <body> <h2& ...
- Chrome插件:弹出桌面通知
if (window.Notification) { var popNotice = function() { if (Notification.permission == "granted ...
- DiscuzX /source/function/function_core.php通用核心函数库文件分析
... <?php /** * [Discuz!] (C)2001-2099 Comsenz Inc. * This is NOT a freeware, use is subject to l ...
- RN 获取组件的宽度和高度
https://www.cnblogs.com/zhiyingzhou/p/7471212.html https://blog.csdn.net/calvin_zhou/article/details ...
- Spring面试问答
1.什么是Spring框架?Spring框架有哪些主要模块? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台.Spring帮助开发者解决了开发中基础性的问题, ...
- spring @Configuration的使用
参考博客:https://www.cnblogs.com/duanxz/p/7493276.html spring中的@Scope注解 https://www.cnblogs.com/loneclo ...
- numpy和matploptlib
numpy Numpy介绍 编辑 一个用python实现的科学计算,包括:1.一个强大的N维数组对象Array:2.比较成熟的(广播)函数库:3.用于整合C/C++和Fortran代码的工具包:4.实 ...
- C# Winform开发程序调用VLC播放器控件播放视频.
VLC是个好东西,支持的格式多,还无广告,关键还有调用它的播放控件不用安装. 开个文章记录下调用这个控件的流水账,以便以后需要的时候查阅 创建工程 首先新建一个Winform工程. 这里姑且叫做VLC ...
- C语言排序算法学习笔记——交换类排序
交换类排序:根据序列中两个元素关键字的比较结果来交换他俩在序列中的位置. 冒泡排序:假设待排序表长为n,从后往前(或从前往后)两两比较相邻元素的值,若为逆序(即A[i-1]>A[i])则交换他们 ...
- js实现输入某串数字,构建完全二叉树,并判断是否为二叉搜索树
思路:若为二叉搜索树,则中序遍历为递增的 let arr = [15,8,16,6,10];let pindex = [];function Node(){ this.root = null; thi ...