제출 #300961

#제출 시각아이디문제언어결과실행 시간메모리
300961kaplanbarZalmoxis (BOI18_zalmoxis)C++17
0 / 100
181 ms262148 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N = 2e6 + 5;
struct Element {
    int x, l, r;
    bool operator<(Element other) const {
        if(x == other.x) {
            return l > other.l;
        }
        return x > other.x;
    }
};
int n, k, a[N];
stack<int> v[N];
priority_queue<Element> q;
int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> k;
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        q.push((Element){a[i], i, i});
    }
    int total = k;
    while(1) {
        if(q.top().x == 30) break;
        int mn = q.top().x;
        pair<int,int> le = make_pair(q.top().l, q.top().r);
        q.pop();
        if(mn == q.top().x && le.second == q.top().l - 1) {
            auto val = (Element){mn + 1, le.first, q.top().r};
            q.pop();
            q.push(val);
        }
        else {
            v[le.second + 1].push(mn);
            total--;
            q.push((Element){mn + 1, le.first, le.second});
        }
    }
    for(int i = 0; i <= n; i++) {
        stack<int> s;
        // Reverse the stack to process in correct order
        while(v[i].size()) {
            s.push(v[i].top());
            v[i].pop();
        }
        while(s.size()) {
            while(total > 0 && s.top() > 0) {
                total--;
                int x  = s.top();
                s.pop();
                s.push(x - 1);
                s.push(x - 1);
            }
            cout << s.top() << " ";
            s.pop();
        }
        if(i != n) cout << a[i] << " ";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...