题目链接:https://codeforces.com/contest/1364/problem/C

题意

给出大小为 $n$ 的非递减数组 $a$,构造同样大小的数组 $b$,使得对于每个 $i$,$b_1, b_2, \ldots, b_i$ 中未出现的最小正整数均为 $a_i$ 。($1 \le n \le 10^5, 0 \le a_i \le i, 0 \le b_i \le 10^6$)

题解

一个比较重要的结论:

\begin{equation} if\ a_i \neq a_{i-1}\ then\ b_i = a_{i - 1} \end{equation}

之后将未出现在数组 $a$ 中或更大的数依次加入数组 $b$ 中即可。

证明

$a_{i - 1}$ 不能等于 $b_j(j < i)$,否则对于 $i - 1$ 不满足题意。

$a_{i - 1}$ 不能等于 $b_j(j > i)$,否则,因为数组 $a$ 为非递减数组,对于 $i$ 不满足题意。

又因为 $a_{i - 1} \neq a_i, a_{i - 1}$ 对于 $a_i$ 是必须的,所以 $b_i = a_{i - 1}$ 。

代码一

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e5 + 10;
  4.  
  5. int a[N], b[N];
  6. bool ex[N];
  7.  
  8. int main() {
  9. int n; cin >> n;
  10. for (int i = 1; i <= n; i++)
  11. cin >> a[i];
  12. memset(b, -1, sizeof b);
  13. for (int i = 1; i <= n; i++) {
  14. if (a[i] != a[i - 1]) {
  15. b[i] = a[i - 1];
  16. ex[b[i]] = true;
  17. }
  18. }
  19. ex[a[n]] = true; //防止全部相同的情况
  20. int num = 0;
  21. for (int i = 1; i <= n; i++) {
  22. if (b[i] == -1) {
  23. while (ex[num]) num++;
  24. b[i] = num++;
  25. }
  26. }
  27. for (int i = 1; i <= n; i++)
  28. cout << b[i] << " \n"[i == n];
  29. }

代码二

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e5 + 10;
  4.  
  5. int a[N], not_in[N];
  6. bool ex[N];
  7.  
  8. int main() {
  9. int n; cin >> n;
  10. for (int i = 1; i <= n; i++)
  11. cin >> a[i], ex[a[i]] = true;
  12. int p = 0;
  13. for (int i = 1; i <= n; i++)
  14. if (!ex[i]) not_in[p++] = i;
  15. int j = 0;
  16. for (int i = 1; i <= n; i++)
  17. cout << (a[i] != a[i - 1] ? a[i - 1] : not_in[j++]) << " \n"[i == n];
  18. }

Codeforces Round #649 (Div. 2) C. Ehab and Prefix MEXs的更多相关文章

  1. Codeforces Round #649 (Div. 2) C. Ehab and Prefix MEXs (构造,贪心)

    题意:有长度为\(n\)的数组\(a\),要求构造一个相同长度的数组\(b\),使得\({b_{1},b_{2},....b_{i}}\)集合中没有出现过的最小的数是\(a_{i}\). 题解:完全可 ...

  2. Codeforces Round #628 (Div. 2) C. Ehab and Path-etic MEXs(树,思维题)

    题意: 给有 n 个点的树的 n-1 条边从 0 到 n-2 编号,使得任意两点路径中未出现的最小数最小的方案. 思路: 先给所有度为 1 的点所在边编号,之后其他点可以随意编排. #include ...

  3. Codeforces Round #649 (Div. 2)

    Codeforces Round #649 (Div. 2) -- WKL \(\mathcal{A}\)题: \(\mathrm{XXXXX}\) Greedy implementation *12 ...

  4. Codeforces Round #649 (Div. 2) C、Ehab and Prefix MEXs D、Ehab's Last Corollary 找环和点染色

    题目链接:C.Ehab and Prefix MEXs 题意; 有长度为n的数组a(下标从1开始),要求构造一个相同长度的数组b,使得b1,b2,....bi集合中没有出现过的最小的数是ai. mex ...

  5. Codeforces Round #525 (Div. 2) F. Ehab and a weird weight formula

    F. Ehab and a weird weight formula 题目链接:https://codeforces.com/contest/1088/problem/F 题意: 给出一颗点有权值的树 ...

  6. Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem

    E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...

  7. Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem

    D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...

  8. Codeforces Round #525 (Div. 2)B. Ehab and subtraction

    B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...

  9. Codeforces Round #525 (Div. 2)A. Ehab and another construction problem

    A. Ehab and another construction problem 题目链接:https://codeforc.es/contest/1088/problem/A 题意: 给出一个x,找 ...

随机推荐

  1. Navcat连接Mysql报错1521

    原因是新版的MySql密码加密算法改变,导致旧版本的Navcat连接报错. 解决方案: 1.升级Navcat 因为只是本地的Mysql,所以没验证这个方法,网传可以,此处不介绍. 2.修改Mysql加 ...

  2. Python 中的行向量、列向量和矩阵

    1.一维数组 一维数组既不是行向量,也不是列向量. import numpy as npa=np.array([1,2,3])print(np.shape(a))>>>(3,) 2. ...

  3. maven 报的一堆错

    今天初学maven,刚开始下载的是Apache-maven-3.6.2然后配置运行一个servlet,但是在pom.xml中写jar包坐标时一直报错显示红色,本地仓库和官网上的中央仓库都试过了就是依赖 ...

  4. Spring Boot超详细用户管理项目(零)——开发前准备

    开始前的软件准备:(编写中:未完成) 使用软件介绍: Java版本:Java SE 11(LTS) 开发工具:IDEA(2020.3版本) Linux系统: 数据库: Java 版本:Java SE ...

  5. MySQL select 子查询的使用

    ### 子查询 >where 这个值是计算出来的 >本质:`在 where 语句中嵌套一个子查询语句` ```sql /*============== 子查询 ============== ...

  6. 【ORA】ORA-01078和LRM-00109 解决方法

    今天切换到asm实例的时候,发现是一个空实例,尝试启动实例,结果报错ORA-01078和LRM-00109 SQL> startupORA-01078: failure in processin ...

  7. Windows+.Net Framework+svn+IIS在Jenkins上的自动化部署入门

    关于Jenkins的使用及安装,上一篇文章我已经介绍过了,Windows+.NetCore+git+IIS在Jenkins上的自动化部署入门.这篇主要是在jenkins如何安装SVN和MSBuild. ...

  8. windows下的:开始→运行→命令

    开始→运行→命令 集锦                          winver---------检查Windows版本wmimgmt.msc----打开windows管理体系结构(WMI)wu ...

  9. pscp 从win10远程传输文件到centos7,多个虚拟机之间传文件

    一.将下载的pscp.exe拷贝到C:\Windows\System32 上传文件 win10 --> linux1 C:\Users\xy>pscp C:\BaiduNetdiskDow ...

  10. Linux 安装分区设置分区大小

    一.Linux分区挂载点介绍 Linux分区挂载点介绍,推荐容量仅供参考不是绝对,跟各系统用途以及硬盘空间配额等因素实际调整: 分区类型 介绍 备注 /boot 启动分区 一般设置100M-200M, ...