题意:给你n个数字,其中可能有相同的数字,要求你用其他的数字替换这些相同的数字,使得所得的序列字典序最小。

Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
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!的更多相关文章

  1. Codeforces Round #436 (Div. 2)【A、B、C、D、E】

    Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张 ...

  2. 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 ...

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

    http://codeforces.com/contest/864/problem/D 题意: 给出n和n个数(ai <= n),要求改变其中某些数,使得这n个数为1到n的一个排列,首先保证修改 ...

  4. 【贪心】Codeforces Round #436 (Div. 2) D. Make a Permutation!

    题意:给你一个长度为n的数组,每个元素都在1~n之间,要你改变最少的元素,使得它变成一个1~n的排列.在保证改动最少的基础上,要求字典序最小. 预处理cnt数组,cnt[i]代表i在原序列中出现的次数 ...

  5. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  6. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...

  7. Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)

    题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...

  8. 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 ...

  9. Codeforces Round #436 (Div. 2) E. Fire

    http://codeforces.com/contest/864/problem/E 题意: 有一堆物品,每个物品有3个属性,需要的时间,失效的时间(一开始)和价值.只能一件一件的选择物品(即在选择 ...

随机推荐

  1. Eclipse导入hadoop源码

    在windows中,使用Eclipse阅读hadoop源码,首先到apache官网下载tar.gz的hadoop源码压缩文件,解压. 方法1:(hadoop技术内幕推荐) 打开Eclipse,新建ja ...

  2. django orm 管理器 objects

    给某张表的管理器重命名 class User(models.Model): name = models.CharField(max_length=100) people = models.Manage ...

  3. 发现一个非常有趣好用的git博主,收录热门OC、swift项目三方架构

    日常学习: https://github.com/iOShuyang/Book-Recommend-Github

  4. 爬虫系列2:Requests+Xpath 爬取租房网站信息

    Requests+Xpath 爬取租房网站信息 [抓取]:参考前文 爬虫系列1:https://www.cnblogs.com/yizhiamumu/p/9451093.html [分页]:参考前文 ...

  5. springboot项目如何打包成war包

    一.修改打包形式 在pom.xml里设置 <packaging>war</packaging> 二.移除嵌入式tomcat插件 在pom.xml里找到spring-boot-s ...

  6. Chrome 58.xxx,iisExpress 创建的SSL网站显示安全正式错误Error:net::ERR_CERT_COMMON_NAME_INVALID

    https://www.reddit.com/r/sysadmin/comments/677hep/chrome_58_not_supporting_self_signed_certificates/ ...

  7. web前端基础学习路线

    1.HTML和CSS的基础知识,完成网页的初步设计 2.JavaScript基础知识和DOM.BOM的学习 3.前端基础框架:CSS框架Bootstrap.JavaScript框架jquery的熟悉使 ...

  8. java利用反射交换两个对象中的字段相同的字段值

    有时候我们的两个对象字段都是一样的,只有极少的区别,想要把一个对象字段的值,赋值给另外一个对象值 然后传给另外一个方法使用,但是这个字段太多,一个一个的复制太过繁琐. 这时候利用反射解决这个问题. c ...

  9. 如何确保Memcache数据读写操作的原子性(转)

    什么是CAS协议 Memcached于1.2.4版本新增CAS(Check and Set)协议类同于Java并发的CAS(Compare and Swap)原子操作,处理同一item被多个线程更改过 ...

  10. 使用GSON工具实现json和其他数据格式间转换

    1 package gson; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.google.gson. ...