Submission #658033

#TimeUsernameProblemLanguageResultExecution timeMemory
658033stevancvZalmoxis (BOI18_zalmoxis)C++14
100 / 100
148 ms14320 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 3e3 + 2;
const int inf = 2e9;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n + 1);
    a[n] = 30;
    stack<int> s;
    vector<pair<int, int>> ans;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i <= n; i++) {
        if (i == 0) {
            ans.push_back({a[i], 0});
            s.push(a[i]);
            continue;
        }
        while (s.top() < a[i]) {
            int x = s.top();
            ans.push_back({x, 1});
            k--;
            s.push(x);
            while (s.size() > 1) {
                x = s.top(); s.pop();
                if (x == s.top()) s.top()++;
                else {s.push(x); break;}
            }
        }
        int x = a[i];
        ans.push_back({a[i], 0});
        s.push(a[i]);
        while (s.size() > 1) {
            x = s.top(); s.pop();
            if (x == s.top()) s.top()++;
            else {s.push(x); break;}
        }
    }
    function<void(int)> F = [&] (int x) {
        if (x == 0 || k == 0) {
            cout << x << sp;
            return;
        }
        k -= 1;
        F(x - 1); F(x - 1);
    };
    for (auto u : ans) {
        if (u.first == 30) break;
        if (u.second == 0) cout << u.first << sp;
        else F(u.first);
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...