# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
50226 | 2018-06-08T11:25:33 Z | rondojim | medians (balkan11_medians) | C++17 | 0 ms | 0 KB |
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; int A[2 * MAXN], B[MAXN], N, cnt[2 * MAXN]; set<int> S; int main(){ scanf("%d", &N); for(int i=1; i<=N; ++i){ scanf("%d", &B[i]); cnt[B[i]]++; } for(int i=1; i<2*N; ++i) S.insert(i); A[1] = B[1], cnt[B[1]]--; S.erase(S.find(B[1])); for(int i=2; i<=N; ++i){ int p = B[i - 1], c = B[i]; set<int>::iterator it; if(c > p){ for(int j=2*i-1; j>=2*i-2; --j){ auto it = S.lower_bound(B[i]); A[j] = *it; S.erase(it); } } else if(c < p){ for(int j=2*i-1; j>=2*i-2; --j){ if(S.find(c) != S.end()){ A[j] = c; S.erase(S.find(c)); } else{ it = S.lower_bound(B[i - 1]); if(it == S.end()) --it; else if(*it >= B[i - 1]) --it; A[j] = *it; S.erase(it); } } else{ it = S.upper_bound(B[i]); A[2 * i - 1] = *it; S.erase(it); it = S.lower_bound(B[i]); if(it == S.end()) --it; else if(*it >= B[i]) --it; A[2 * i - 2] = *it; S.erase(it); } } for(int i=1; i<2*N; ++i) printf("%d ", A[i]); return 0; }