Codeforces Round #436 (Div. 2) D. Make a Permutation!
来源:互联网 发布:阿里云搭建个人博客 编辑:程序博客网 时间:2024/06/05 23:42
题意:
现在有n个数字(大小从1-n),要你改变其中一些数字,使得1-n都只出现一次,在保证改变次数最小的情况下,输出字典序最小的情况
思路:
我们可以用一个数组b表示某个数字出现的个数(例如2出现了3次,则b[2]=3),那么,我们可以通过扫一次b数组,就能得知我需要填入的数字,可是,而我从1-n地扫,就能保证我在改变原数组时候供我填入的数字是目前最小的,然后我们就可以开始扫原数组了。
如果原数组当前的这个数字出现了一次以上且比我现在要填入的数字还要小,那么我可以选择跳过来保证字典序最小,可是,对于一个出现了x次的数字来说,它出现的x-1次都必须改变,那么这个数字也只能被跳过一次,我们可以用一个数组来表示是否还允许跳过。这样的话这个问题就能o(n)的解决了。
错误和反思:
2 WA。在当时打CF的时候,思路立马就有了,可是实现的时候有点乱,其中的几个逻辑顺序搞混了好几次,结果硬是改了半个小时,果然还是代码能力不够啊,ORZ
代码:
#include<bits/stdc++.h>using namespace std;const int maxn=200000+1000;int n;int a[maxn];int b[maxn];bool c[maxn];int jx[maxn];int main(){ scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a[i]); b[a[i]]++; } memset(c,false,sizeof(c)); int cnt=0; vector<int> q; for(int i=1;i<=n;i++) { if(b[i]==0) q.push_back(i); if(b[i]>1) jx[i]=b[i]-1; } int now=0; for(int i=0;i<n;i++) { if(c[a[i]]) { a[i]=q[now]; now++; cnt++; } else if(b[a[i]]>1) { if(q[now]<a[i]) { b[a[i]]--; jx[a[i]]--; a[i]=q[now]; now++; cnt++; } else{ b[a[i]]--; if(b[a[i]]==jx[a[i]]) { c[a[i]]=true; } } } } printf("%d\n",cnt); for(int i=0;i<n;i++) { printf("%d ",a[i]); } puts("");}
阅读全文
0 0
- Codeforces Round #436 (Div. 2)D. Make a Permutation! codeforces-864D. Make a Permutation!
- D. Make a Permutation! (Codeforces Round #436 (Div. 2))
- Codeforces Round #436 (Div. 2) D. Make a Permutation!
- Codeforces Round #436 (Div. 2)-贪心-D. Make a Permutation!
- Codeforces Round #436 (Div. 2) 864D.Make a Permutation!
- Codeforces Round #436 (Div. 2) D. Make a Permutation!
- Codeforces Round #436 (Div. 2) D. Make a Permutation!
- Codeforces Round #436 (Div. 2) D. Make a Permutation!(优先队列)
- Codeforces Round #436 (Div. 2) D. Make a Permutation! 思维+贪心
- Codeforces Round #436 (Div. 2) Make a Permutation!
- Codeforces Round #436 (Div. 2) Make a Permutation!
- Codeforces Round #436 (Div. 2) A.Fair Game B.Polycarp and Letters C. Bus D.Make a Permutation!
- Codeforces Round #436 D.Make a Permutation!(贪心)
- Codeforces 864D Make a Permutation!
- CodeForces 864D Make a Permutation!
- Codeforces 864 D Make a Permutation!
- Codeforces Round #209 (Div. 2) <A-D>
- Codeforces Round #231 (Div. 2)A-D
- Tomcat修改端口仍然不能启动的问题
- Python+Selenium【3】自动化介绍
- Python---异常处理try...except、raise
- iOS Android微信及浏览器中唤起本地APP
- A cycle was detected in the build path of project 'core'. The cycle consists of projects {core, sms}
- Codeforces Round #436 (Div. 2) D. Make a Permutation!
- 一种基于RSI和K线的择时策略
- js Array.prototype.indexOf 获取元素在数组中的index
- Android View 四个构造函数作用
- 某安全浏览器竟然也被查出高危漏洞?开源安全问题不容忽视
- C# TimeSpan 的 Days、TotalDays、Hours、TotalHours
- JSP中forward和redirect有什么区别? 什么时候必须用哪个?
- webstorm导入背景、字体等的配置文件
- Jmeter 让CSV Data Set Config能使用Jmeter已经有的变量数据