제출 #139047

#제출 시각아이디문제언어결과실행 시간메모리
139047mlyean00Zalmoxis (BOI18_zalmoxis)C++17
95 / 100
1049 ms34484 KiB
#ifdef DEBUG
#include "debug.hpp"
#else
#pragma GCC optimize("Ofast")
#define trace(...)
#include <bits/stdc++.h>
#define endl '\n'
#endif

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k;
    cin >> n >> k;

    list<int> a;
    int rem = 1 << 30;
    for (int i = 0; i < n; ++i) {
        int tmp;
        cin >> tmp;
        a.push_back(tmp);
        rem -= 1 << tmp;
    }

    stack<int> st;
    st.push(30);
    for (auto it = a.begin(); it != a.end(); ++it) {
        if (*it < st.top()) {
            st.push(*it);
        } else if (*it == st.top()) {
            int h = st.top();
            while (h == st.top()) {
                st.pop();
                if (h == 30) break;
                ++h;
            }
            st.push(h);
        } else {
            int h = st.top();
            rem -= 1 << h;
            it = a.insert(it, h);
            while (h == st.top()) {
                st.pop();
                if (h == 30) break;
                ++h;
            }
            st.push(h);
        }
    }

    list<int> b;
    while (st.top() != 30) {
        int h = st.top();
        rem -= 1 << h;
        b.push_back(h);
        while (h == st.top()) {
            st.pop();
            if (h == 30) break;
            ++h;
        }
        st.push(h);
    }

    assert(rem >= 0);

    for (; rem; rem -= rem & -rem) {
        b.push_back(__builtin_ctz(rem));
    }

    int left = (n + k) - (a.size() + b.size());
    assert(left >= 0);

    auto it = b.begin();
    for (int i = 0; i < left; ++i) {
        while (*it == 0)
            ++it;
        --(*it);
        it = b.insert(it, *it);
    }

    a.splice(a.end(), b);

    copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...