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个属性,需要的时间,失效的时间(一开始)和价值.只能一件一件的选择物品(即在选择 ...
随机推荐
- obtainFreshBeanFactory()源码探究
该方法目的是获取bean工厂.主要逻辑是:刷新bean工厂,获取bean工厂,进而返回bean工厂,但实际上并没有刷新bean工厂,基本上为空实现.源码如下: 而刷新bean工厂,正如注释所说,do ...
- CleanMyMac X教程之-安装卸载
Mac清理软件CleanMyMac X的出现成功的吸引了Mac用户的注意,CleanMyMac X是2018年发布的,深受许多Mac用户的青睐.windows操作端有360等众多清洁软件,那么Mac端 ...
- OPC客户端开发问题总结
环境准备 采用MatrikonOPC做模拟服务器,注册 OPCDAAuto.dll组件 引用 Interop.OPCAutomation.dll组件.开始开发. 1..new OPCServer()- ...
- 7.11 animals.c 程序
7.11 animals.c 程序 #include <stdio.h> #include <ctype.h> int main(void) { char ch; printf ...
- activity--常见属性总结
15.Activit的几个重要属性总结? 12.onNewIntent()使用Tips?11.launchMode的属性介绍?及其常用的Intent Flag? ==== 15.Activit的几个重 ...
- linux 退出当前命令的编辑
有时候输入命令,linux既不响应命令,也不提示错误.可以用如下方法结束当前命令: ctrl+z挂起当前进程如果需要恢复到前台输入fg,恢复到后台输入bgctrl+c,彻底终止该进程
- stm32 uart 中断 蜜汁bug
在项目中,使用stm32f103,配置uart1接收RXNE中断,使用DMA来进行UART1的发送. 初始化代码如下: void uart_init(u32 bound) { GPIO_InitTyp ...
- Java单例模式之懒汉模式线程安全
单例模式是设计模式中用得比较多的一种设计模式,它的主要优点有: 1.访问受控,保证访问的是唯一的实例. 2.由于只有一个实例,所以节省资源. 缺点: 灵活性低,如果对象的应用场景多变,则不适用单例模式 ...
- gentoo hibernate
首先修改内核: Power management and ACPI options ---> [*] Suspend to RAM and standby [*] Hibernation (ak ...
- java 连接SQL Server
1.确认服务器的连通性,并且使用账户密码模式登陆有效. 1).登陆服务器 2).查看安全性 2.新建数据库用于测试 3.下载jdbc安装并配置 进入微软官网主页--> 搜索JDBC-->找 ...