标签:include 构造 ldo ems clu namespace 整数 $1 bit
题目链接: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_{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
标签:include 构造 ldo ems clu namespace 整数 $1 bit
原文地址:https://www.cnblogs.com/Kanoon/p/13125899.html