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$)

题解

一个比较重要的结论:

egin{equation} if a_i eq 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} eq 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] << " 
"[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++]) << " 
"[i == n];
}
原文地址:https://www.cnblogs.com/Kanoon/p/13125899.html