Submission #1339373

#TimeUsernameProblemLanguageResultExecution timeMemory
1339373kyrylmedians (balkan11_medians)C++20
100 / 100
60 ms12288 KiB
#include <bits/stdc++.h>
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define per(i, a, b) for(ll i = a; i >= b; i--)
#define all(v) begin(v), end(v)
#define st first
#define nd second
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
const int N = 1e5 + 5;
int tab[N];
int odp[N * 2 + 5];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    set<int> ava;
    rep(i, 0, n){
        ava.insert(2 * i + 1);
        if(2 * i + 2 != n * 2) ava.insert(2 * i + 2);
        cin >> tab[i];
    }
    odp[0] = tab[0];
    ava.erase(tab[0]);
    rep(i, 1, n){
        // cout << ava.size() << endl;
        if(tab[i] == tab[i - 1]){
            odp[i * 2 - 1] = *ava.begin();
            odp[i * 2] = *prev(ava.end());
            ava.erase(ava.begin());
            ava.erase(prev(ava.end()));
        }
        else if(tab[i] > tab[i - 1]){
            if(ava.contains(tab[i])){
                odp[i * 2 - 1] = tab[i];
                odp[i * 2] = *prev(ava.end());
                ava.erase(prev(ava.end()));
                ava.erase(ava.find(tab[i]));
            }
            else{
                odp[i * 2 - 1] = *prev(ava.end());
                ava.erase(prev(ava.end()));
                odp[i * 2] = *prev(ava.end());
                ava.erase(prev(ava.end()));
            }
        }
        else{
            if(ava.contains(tab[i])){
                odp[i * 2 - 1] = tab[i];
                odp[i * 2] = *ava.begin();
                ava.erase(ava.begin());
                ava.erase(ava.find(tab[i]));
            }
            else{
                odp[i * 2 - 1] = *ava.begin();
                ava.erase(ava.begin());
                odp[i * 2] = *ava.begin();
                ava.erase(ava.begin());
            }
        }
    }
    rep(i, 0, n * 2 - 1){
        cout << odp[i] << ' ';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...