Codeforces Round #649 (Div. 2) C. Ehab and Prefix MEXs
题目链接: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的更多相关文章
- Codeforces Round #649 (Div. 2) C. Ehab and Prefix MEXs (构造,贪心)
题意:有长度为\(n\)的数组\(a\),要求构造一个相同长度的数组\(b\),使得\({b_{1},b_{2},....b_{i}}\)集合中没有出现过的最小的数是\(a_{i}\). 题解:完全可 ...
- Codeforces Round #628 (Div. 2) C. Ehab and Path-etic MEXs(树,思维题)
题意: 给有 n 个点的树的 n-1 条边从 0 到 n-2 编号,使得任意两点路径中未出现的最小数最小的方案. 思路: 先给所有度为 1 的点所在边编号,之后其他点可以随意编排. #include ...
- Codeforces Round #649 (Div. 2)
Codeforces Round #649 (Div. 2) -- WKL \(\mathcal{A}\)题: \(\mathrm{XXXXX}\) Greedy implementation *12 ...
- 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 ...
- 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 题意: 给出一颗点有权值的树 ...
- 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 题意: 给出一个 ...
- 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 ...
- Codeforces Round #525 (Div. 2)B. Ehab and subtraction
B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...
- 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,找 ...
随机推荐
- springboot异常处理之404
ps: 推荐一下本人的通用后台管理项目crowd-admin 以及newbee-mall增强版,喜欢的话给个star就好 源码分析 在springboot中默认有一个异常处理器接口ErrorConto ...
- Linux 入门教程:00 Background
Linux 为何物? 就是一个操作系统. Linux 历史: 操作系统始于二十世纪五十年代,当时的操作系统能运行批处理程序.批处理程序不需要用户的交互,它从文件或者穿孔卡片读取数据,然后输出到另外一个 ...
- 区间合并 C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; ty ...
- iTerm2 实现 ssh 自动登录,并使用 Zmodem 实现快速传输文件
原文链接:https://fuckcloudnative.io/posts/iterm2-auto-login/ 对于 YAML 工程师来说,我们经常需要 ssh 登录不同的服务器,每次登录时都要经历 ...
- 面试常问的ArrayQueue底层实现
public class ArrayQueue<T> extends AbstractList<T>{ //定义必要的属性,容量.数组.头指针.尾指针 private int ...
- winform 扫码识别二维码
因为公司业务需求,需要在Windows系统下调用摄像头识别二维码需求,就有了这个功能. 我根据网上网友提供的一些资料,自己整合应用到项目中,效果还不错(就是感觉像素不是太好) 现在将调用摄像头+识别二 ...
- 白日梦的Elasticsearch实战笔记,32个查询案例、15个聚合案例、7个查询优化技巧。
目录 一.导读 三._search api 搜索api 3.1.什么是query string search? 3.2.什么是query dsl? 3.3.干货!32个查询案例! 四.聚合分析 4.1 ...
- windows10复制粘贴键突然失效无法复制粘贴的最简单办法
报了学习班,打开了VCE的加密文档 今天复制粘贴键突然失效 在网上捣鼓了好多方法都不行最后发现看看你有没有在用加密文件,也就是网课类的文档和视频.有就把它关了关了就好了
- selenium元素等待的三种方法
1.强制等待sleep() 使用方法:sleep(X),等待X秒后,进行下一步操作. 使用最简单的一种办法就是强制等待sleep(X),强制让浏览器等待X秒,不管当前操作是否完成,是否可以进行下一步操 ...
- spark开窗函数
源文件内容示例: http://bigdata.beiwang.cn/laoli http://bigdata.beiwang.cn/laoli http://bigdata.beiwang.cn/h ...