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个属性,需要的时间,失效的时间(一开始)和价值.只能一件一件的选择物品(即在选择 ...
随机推荐
- Windos下pip配置豆瓣源
C:\Users\liche\pip 下创建pip.ini文件 pip.ini [global] index-url = http://pypi.douban.com/simple [install] ...
- LeetCode【110. 平衡二叉树】
对于平衡二叉树,就是左右深度相差1 就可以另外弄一个函数,计算深度,然后, 在原函数上进行比较深度是否相差1,再输出true or false. 至于迭代就可以,比较完左右节点,再比较各自的左右节点. ...
- Win2008服务启动不能调用Office Word的解决方法
本文为大家分享一下如何解决Windows Server 2008 服务启动不能调用Office Word的问题,分享这个教程的原因是,今天在Windows server2008上部署一个应用时发现了一 ...
- WebApp的自动测试工具: Jenkins
一.下载并安装(msi)https://jenkins.io/download/thank-you-downloading-windows-installer-stable/ 在安装过程这, 需要从p ...
- POJ1083 Moving Tables
POJ1083 Moving Tables Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35297 Accepted: ...
- 常见26种NLP任务的练手项目
经常有人问我:老大让我完成xxx,我不会,他也不会,但是很着急.这个任务怎么实现啊?这个任务需要什么技术啊?这种情况我遇到有100+次了,而且很多时候问得问题跟具体需要的技术简直是驴唇不对马嘴.所以今 ...
- DLC 格雷码
格雷码特点 每相邻两个数,只会有一位发生变(二进制数) 异或运算 若两个运算数相同,结果为 0 若两个运算数不相同, 结果为 1
- Tomcat 控制台出现乱码
本地在启动tomcat时,控制台启动显示乱码 这是因为windows默认编码集为GBK,用startup.bat启动tomcat时,它会读取catalina.bat的代码并打开一个新窗口运行,打开的c ...
- leetcode每日刷题计划-简单篇day11
Num 121 买卖股票的最佳时期 Best Time to Buy and Sell Stock class Solution { public: int maxProfit(vector<i ...
- 68.纯 CSS 创作一本色卡
原文地址:https://segmentfault.com/a/1190000015456554 感想:复习下 transform-origin: 位置; HTML code: <div cla ...