题目链接:https://codeforces.com/contest/1375/problem/D

题意

给出一个大小为 $n$,元素值位于 $[0,n]$ 之间的数组,每次可以将一个元素替换为数组中未出现过的最小非负整数,最多替换 $2n$ 次,输出一种使得数组为非递减数组的替换方案。

题解

将数组替换为 $0, 1, 2, \dots, n - 1$ 即可,每个 $a_i$ 最多替换两次,一次为 $n$,一次为 $i$ 。

代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1010; int n, a[N]; int MEX() {
int cnt[n + 1] = {};
for (int i = 0; i < n; i++)
++cnt[a[i]];
for (int i = 0; i <= n; i++)
if (cnt[i] == 0) return i;
} void solve() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
vector<int> res;
while (!is_sorted(a, a + n)) {
int mex = MEX();
if (mex == n) {
for (int i = 0; i < n; i++) {
if (a[i] != i) {
a[i] = n;
res.push_back(i);
break;
}
}
} else {
a[mex] = mex;
res.push_back(mex);
}
}
cout << res.size() << "\n";
for (auto i : res) cout << i + 1 << ' ';
cout << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

Codeforces Global Round 9 D. Replace by MEX的更多相关文章

  1. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  2. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  3. Codeforces Global Round 2 题解

    Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...

  4. Codeforces Global Round 1 (A-E题解)

    Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...

  5. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

  6. Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)

    Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...

  7. 【手抖康复训练1 】Codeforces Global Round 6

    [手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...

  8. Codeforces Global Round 11 个人题解(B题)

    Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...

  9. 【Codeforces Round 1110】Codeforces Global Round 1

    Codeforces Round 1110 这场比赛只做了\(A\).\(B\).\(C\),排名\(905\),不好. 主要的问题在\(D\)题上,有\(505\)人做出,但我没做出来. 考虑的时候 ...

随机推荐

  1. 关于es6 let var const 以及Symbol的总结

    ```javascript //es6新增块级作用域.声明变量用关键字let const , // es5中只有函数作用域和全局作用域,声明变量用关键字var  // let 和const 声明的变量 ...

  2. Java 中 Executors.newSingleThreadExecutor() 与Executors.newFixedThreadPool(1)有什么区别

    在研究Executors提供的线程池时自然会想到标题这个问题,既然已经有了newFixedThreadPool,为什么还要存在newSingleThreadExecutor这个方法.难道newFixe ...

  3. nginx: [emerg] bind() to 0.0.0.0:80 failed (10013:

    问题出现 今天在win10安装nginx时候,启动nginx.exe时在dos窗口出现了这个错误,特此记录一下. 解决方法 上面报错信息的意思大概是:0.0.0:80地址访问不被允许.可能是80端口号 ...

  4. (二)数据源处理5-excel数据转换实战(上)

    把excel_oper02.py 里面实现的:通过字典的方式获取所有excel数据.放进utils: ️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️ utils: def get_al ...

  5. git revert 回退已经push的内容

    如题,在日常的开发过程中,可能有组员不小心一下子吧文件修改,需要进行回退 回退主要涉及到2种命令,一种是git reset 一种是 git revert git reset 会修改git log提交历 ...

  6. linux之平均负载(学习笔记非原创)

    什么是平均负载 [root@111 ~]# uptime 11:03:33 up 149 days, 17:34, 1 user, load average: 0.08, 0.05, 0.01 最后三 ...

  7. 创建并使用https证书

    目录 前言 产生证书 测试https服务器 用tls加密tcp连接 总结 前言 https要比http更安全些,因此可以配置Nginx服务器使用证书,客户端就会去第三方平台校验证书. 但是我们自己的服 ...

  8. 【故障公告】K8s CofigMap 挂载问题引发网站故障

    今天凌晨我们用阿里云服务器自建的 kubernetes 集群出现突发异常情况,博客站点(blog-web)与博客 web api(blog-api)的 pod 无法正常启动(CrashLoopBack ...

  9. Centos 安装postgreSQL9.4.3

    rpm -ivh http://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7.2-x86_64/pgdg-centos94-9.4-3 ...

  10. 【转载】Linux中找不到ifconfig命令的解决方法

    ifconfig命令出错 ifconfig命令是设置或显示网络接口的程序,可以显示出我们机器的网卡信息,可是有些时候最小化安装CentOS等Linux发行版的时候会默认不安装ifconfig等命令,这 ...