Submission #646132

#TimeUsernameProblemLanguageResultExecution timeMemory
646132Matteo_Verzmedians (balkan11_medians)C++17
10 / 100
109 ms12004 KiB
#include <bits/stdc++.h>
#ifdef BLAT
   #include "debug/debug.hpp"
#else
   #define debug(x...)
#endif

using namespace std;

void nextValue(int &last, int coef, const set <int> &myset) {
    while (myset.find(last) == myset.end())
        last += coef;
}

void insertValue(int value, vector <int> &v, set <int> &myset) {
    v.push_back(value);
    myset.erase(value);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    vector <int> a, b;
    set <int> myset;
    cin >> n;
    b.resize(n);

    cin >> b[0];
    a.push_back(b[0]);
    for (int i = 1; i < 2 * n; i++)
        if (i != b[0])
            myset.insert(i);

    int l = *myset.begin(); 
    int r = *myset.rbegin();

    for (int i = 1; i < n; i++) {
        cin >> b[i];
        nextValue(l, 1, myset);
        nextValue(r, -1, myset);
        if (b[i] == b[i - 1]) {
            insertValue(l, a, myset);
            insertValue(r, a, myset);
        } else {
            if (myset.find(b[i]) != myset.end()) {
                a.push_back(b[i]);
                myset.erase(b[i]);

                if (l - 1 > 2 * n - 1 - r) {
                    nextValue(r, -1, myset);
                    insertValue(r, a, myset);
                } else {
                    nextValue(l, 1, myset);
                    insertValue(l, a, myset);
                }
            } else for (int j = 0; j < 2; j++) {
                    if (l - 1 > 2 * n - 1 - r) {
                        nextValue(r, -1, myset);
                        insertValue(r, a, myset);
                    } else {
                        nextValue(l, 1, myset);
                        insertValue(l, a, myset);
                    }
                }
        }
    }

    for (auto it : a)
        cout << it << ' ';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...