题目链接: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}$ 。

代码一

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10; int a[N], b[N];
bool ex[N]; int main() {
int n; cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
memset(b, -1, sizeof b);
for (int i = 1; i <= n; i++) {
if (a[i] != a[i - 1]) {
b[i] = a[i - 1];
ex[b[i]] = true;
}
}
ex[a[n]] = true; //防止全部相同的情况
int num = 0;
for (int i = 1; i <= n; i++) {
if (b[i] == -1) {
while (ex[num]) num++;
b[i] = num++;
}
}
for (int i = 1; i <= n; i++)
cout << b[i] << " \n"[i == n];
}

代码二

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10; int a[N], not_in[N];
bool ex[N]; int main() {
int n; cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i], ex[a[i]] = true;
int p = 0;
for (int i = 1; i <= n; i++)
if (!ex[i]) not_in[p++] = i;
int j = 0;
for (int i = 1; i <= n; i++)
cout << (a[i] != a[i - 1] ? a[i - 1] : not_in[j++]) << " \n"[i == n];
}

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. Flutter 布局类组件:弹性布局(Flex)

    前言 弹性布局允许子组件按照一定比例来分配父容器空间,Flutter中的弹性布局主要通过Flex和Expanded来配合实现. Flex Flex组件可以沿着水平或垂直方向排列子组件,如果你知道主轴方 ...

  2. 【Sed】使用sed删除文件指定行的内容

    sed多看帮助文档,受益良多 sed -i '$d' filename 例如删除 /etc/profile的最后一行 cat -n /etc/profile ...    101  export PA ...

  3. 优化太多的if-else

    来源java小当家 第1种方法:提前return,减少else判断 1 // 1.优化前 2 private int handlerPre1(boolean flag) { 3 if(flag){ 4 ...

  4. SpringBoot 好“吃”的启动原理

    原创:西狩 编写日期 / 修订日期:2020-12-30 / 2020-12-30 版权声明:本文为博主原创文章,遵循 CC BY-SA-4.0 版权协议,转载请附上原文出处链接和本声明. 不正经的前 ...

  5. 打包遇到错误Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test

    引自:https://blog.csdn.net/xiexiangyan/article/details/107936774 遇到的问题 有一个maven项目,我clone一下最新的代码.准备打包(m ...

  6. pytorch——不用包模拟简单线性预测,数据类型,创建tensor,索引与切片

    常见的学习种类 线性回归,最简单的y=wx+b型的,就像是调节音量大小.逻辑回归,是否问题.分类问题,是猫是狗是猪 最简单的线性回归y=wx+b 目的:给定大量的(x,y)坐标点,通过机器学习来找出最 ...

  7. 百度文库Word下载器

    最近我妈的文库VIP用完了,但还有很多资源要下载,于是我便在网上找下载工具. 总算找到个完美的!(虽然没界面) 既然没界面,那就自己写一个呗! 原作者 该程序的下载和写入部分由地球守卫者制作 原文链接 ...

  8. 将汉字取模软件中的汉字放到keil5中显示

    最近因为要使用STM32做毕业设计,需要用LCD显示中文,STM32开发板用的是原子的战舰STM32开发板,给的LCD显示例程里貌似没有中文显示,那么需要自己去编写中文显示程序. 软件编写对我来说并不 ...

  9. 说说C# 8.0 新增功能Index和Range的^0是什么?

    前言 在<C# 8.0 中使用 Index 和 Range>这篇中有人提出^0是什么意思?处于好奇就去试了,结果抛出异常.查看官方文档说^0索引与 sequence[sequence.Le ...

  10. 电脑打不开gitHub的解决方法

    电脑打不开gitHub的解决方法 方法:修改本地的hosts文件 因为Github是国外网站,所以经常会遇到打不开的问题,并且有时能打开但是网速好慢 解决这个问题的方法是 :   C:\Windows ...