#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 2e5 + 2;
set < ll > uragsh_ind[N], aragsh_ind[N];
ll uragsh[N], aragsh[N], a[N], b[N], used[N];
int main() {
	ll n, m, r, x,s, y, i, j,ind, ans, t;
	
	cin >> n;
	
	
	
	for (i = 1; i <= n; i ++) {
		cin >> a[i];
		used[a[i]] = 1;
	}
	
	for (i = 1; i <= 2 * n - 1; i ++) b[i]= -1;
	b[1] = a[1];
	for (i = 2; i <= n; i ++) {
		if ( a[i] == a[i - 1]) {
			uragsh[a[i]] = aragsh[a[i]] = 1;
			uragsh_ind[a[i]].insert(i);
			aragsh_ind[a[i]].insert(-i);
		}
		else {
			if ( a[i] > a[i- 1]) {
				aragsh_ind[a[i]].insert(-i);
				aragsh[a[i]] =1;
				b[2 * i - 1] = a[i];
			}
			else {
				uragsh_ind[a[i]].insert(i);
				uragsh[a[i]] = 1;
				b[2 * i - 1] = a[i];
			}
		}
	}
	queue < ll > q;
	for (i =1 ; i <= 2 *n - 1; i ++) {
		if ( used[i] == 1) {
			for ( ll ind : uragsh_ind[i]) {
				s = q.front();
				q.pop();
				b[ind * 2 - 2] = s;
			}	
		}
		else {
			q.push(i);
		}
	}
	stack < ll > v;
	while(!q.empty()) {
		v.push(q.front());
		q.pop();
	}
	for (i = 2 * n - 1; i >= 1; i --) {
		if ( used[i] == 1) {
			for ( ll ind : aragsh_ind[i]) {
				s = v.top();
				v.pop();
				ind =-ind;
				if ( b[ind * 2 - 1] == -1) b[ind * 2 - 1] = s;
				else b[ind * 2 - 2] = s;
			}
		}
	}
	for (i = 1; i <= 2 *n - 1; i ++) {
		if ( b[i] < 1 || b[i] > 2 * n - 1) {
			while(1);
		}
		cout << b[i] << " ";
	}
	cout <<"\n";
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |