Submission #750145

#TimeUsernameProblemLanguageResultExecution timeMemory
750145vqpahmadZalmoxis (BOI18_zalmoxis)C++14
100 / 100
421 ms104284 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long 
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mod = 1e9 + 7;
const int N = 1e6 + 15;
const ll inf = 1e18;

int n,k;
vector<pii> ans;
int cnt = 0;
void pr(int o){
    if (k>0&&o>0){
        k--;
        pr(o-1);
        pr(o-1);
        return ;
    }
    cnt++;
    cout << o << ' ';
}
int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin >> n >> k;
    stack<int> st;
    vector<int> a(n);
    map<int,vector<int>> mp;
    for (int i=0;i<n;i++){
        int x;
        cin >> x;
        a[i] = x;
        if (!i){
            st.push(x);
            continue;
        }
        while (x > st.top()){
            int u = st.top();
            mp[i-1].pb(u);
            while (sz(st)&&u==st.top()){
                st.pop();
                u++;
            }
            st.push(u);
        }
        while (sz(st)&&x==st.top()){
            st.pop();
            x++;
        } 
        st.push(x);
    }
    while (st.top()<30){
        int u = st.top();
        mp[n-1].pb(u);
        while (sz(st)&&u==st.top()){
            st.pop();
            u++;
        }
        st.push(u);
    }
    for (int i=0;i<n;i++){
        ans.push_back({a[i],0});
        for (auto it : mp[i]) {
            ans.push_back({it,1});
            k--;
        }
    }
    while (sz(st)) st.pop();
    n = sz(ans);
    st.push(ans[0].F);
    for (int i=1;i<n;i++){
        int u = ans[i].F;
        while (st.size()&&u==st.top()){
            st.pop();
            u++;
        }
        st.push(u);
    }
    assert(st.top() == 30 && st.size()==1);
    for (int i=0;i<n;i++){
        if (ans[i].S==0){
            cout << ans[i].F << ' ';
        }
        else pr(ans[i].F);
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...