C. Ehab and Prefix MEXs
Given an array aa of length nn, find another array, bb, of length nn such that:
- for each ii (1≤i≤n)(1≤i≤n) MEX({b1MEX({b1, b2b2, ……, bi})=aibi})=ai.
The MEXMEX of a set of integers is the smallest non-negative integer that doesn't belong to this set.
If such array doesn't exist, determine this.
The first line contains an integer nn (1≤n≤1051≤n≤105) — the length of the array aa.
The second line contains nn integers a1a1, a2a2, ……, anan (0≤ai≤i0≤ai≤i) — the elements of the array aa. It's guaranteed that ai≤ai+1ai≤ai+1 for 1≤i<n1≤i<n.
If there's no such array, print a single line containing −1−1.
Otherwise, print a single line containing nn integers b1b1, b2b2, ……, bnbn (0≤bi≤1060≤bi≤106)
If there are multiple answers, print any.
3 1 2 3
0 1 2
4 0 0 0 2
1 3 4 0
3 1 1 3
0 2 1
In the second test case, other answers like [1,1,1,0][1,1,1,0], for example, are valid.
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> #include <unordered_set> #include <unordered_map> #define ll long long #define PII pair<int, int> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dec(i,a,b) for(int i=a;i>=b;i--) using namespace std; int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979323846; const double eps = 1e-6; const int mod =1e9+7; const int N = 1e5+5; //if(x<0 || x>=r || y<0 || y>=c) inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } ll lcm(ll m, ll n) { return m * n / gcd(m, n); } ll qpow(ll m, ll k, ll mod) { ll res = 1, t = m; while (k) { if (k & 1) res = res * t % mod; t = t * t % mod; k >>= 1; } return res; } int n; int a[N], st[N], d[N], m; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], st[a[i]] = 1; for (int i = 1; i <= n; i++) if (!st[i]) d[++m] = i;//用d存下所有缝隙的数 int j = 0; for (int i = 1; i <= n; i++) { if (a[i] == a[i - 1]) cout << d[++j] << " "; else cout << a[i - 1] << " "; } return 0; }