Codeforces Round #436 (Div. 2) Make a Permutation!

来源:互联网 发布:如何优化企业资本结构 编辑:程序博客网 时间:2024/06/05 18:46

题目链接:http://codeforces.com/contest/864/problem/D

题意:给你一个序列,要使不能在序列中出现相同的数字,问你最少要改多少个数字,要求字典序最小

思路:这个题看起来很容易,其实实现起来还是有点麻烦的,首先是找到出现重复的数字,并记录它出现的次数,然后就是用一个数组来从小到大来存可以加进来的数,最后是改数,如果比加的数大就直接换了,如果比加的数小那么第一个出现的数就不改,并且后面的出现的所有这个数都要改了,从而保证只有一个不改变,而顺序是从小到大,具体看代码。


#include <bits/stdc++.h>using namespace std;typedef long long LL;typedef pair<LL, LL> P;const int maxn = 2e5 + 5;const int mod = 1e8 + 7;int n;struct node {  //一开始写复杂了,所以用了个结构体,其实不用的,2333,就当是个普通数组啦,懒得改了    int value;}a[maxn];int num[maxn];int res[maxn];bool vis[maxn];int main() {    //freopen ("in.txt", "r", stdin);    while (~scanf ("%d",&n)){        memset(num,0,sizeof(num));        memset(res,0,sizeof(res));        memset(vis,0,sizeof(vis));        int Max=0;        for (int i=1;i<=n;i++){            scanf ("%d",&a[i].value);            if (a[i].value>Max) Max=a[i].value;            num[a[i].value]++;        }        for (int i=1;i<=n;i++){            if (num[a[i].value]==1) {                num[a[i].value]=-1;            }        }        int cnt=0;        int s=0,ss=0;        for (int i=1;i<=n;i++){            if (num[i]==0) {                res[cnt++]=i;            }            else if (num[i]>=2) {                s++;                ss+=num[i];            }        }        while (cnt<ss-s){            res[cnt++]=++Max;        }        int x=0;        for (int i=1;i<=n;i++){            if (num[a[i].value]>=2) {                if (a[i].value>res[x]){                    num[a[i].value]--;                    a[i].value=res[x];                    x++;                }                else {                    if (vis[a[i].value]==0){                        vis[a[i].value]=1;                        continue;                    }                    num[a[i].value]--;                    a[i].value=res[x];                    x++;                }            }        }        printf ("%d\n",ss-s);        for (int i=1;i<=n;i++){            printf ("%d ",a[i].value);        }        printf ("\n");    }    return 0;}


阅读全文
0 0
原创粉丝点击