#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 3;
int n, ans[N], b[N], vis[N];
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = n, pos = 2*n - 1, pre = 0; i >= 1; i--){
if (b[i] != pre){
ans[pos] = b[i];
vis[b[i]] = 1;
pre = b[i];
}
pos -= 2;
}
int sm = 1, bg = 0, s = 1, e = 2 * n - 1;
for (int i = 2; i <= 2*n - 1; i++){
if (ans[i]) {
sm++;
} else{
if (sm < bg) {
while (vis[s]) s++;
vis[s] = 1;
ans[i] = s;
sm++;
} else {
while (vis[e]) e--;
vis[e] = 1;
ans[i] = e;
bg++;
}
}
}
for (int i = 1; i <= 2*n - 1; i++) cout << ans[i] << " ";
cout << "\n";
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |