Submission #925008

#TimeUsernameProblemLanguageResultExecution timeMemory
925008parlimoosRLE (BOI06_rle)C++14
100 / 100
1649 ms115224 KiB
//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<ll , x>
#define endl '\n'

ll n , m;
vector<arr(2)> bls;
set<arr(2)> ord;
ll infs[100000];
ll mns[2000000][2];
vector<ll> o;

void add(ll c , ll x){
    ll len = (ll)bls.size();
    if(!bls.empty() and bls[len - 1][0] == c) bls[len - 1][1] += x;
    else bls.pb({c , x});
}
ll clc(ll i , bool sgn){
    if(sgn) return (3 * ((bls[i][1] + n - 1) / n));
    ll res = 3 * (bls[i][1] / (n + 2));
    if(bls[i][1] % (n + 2) > 2) res += 3;
    else res += bls[i][1] % (n + 2);
    return res; 
}
void clcO(ll i , ll sgn){
    if(sgn == bls[i][0]){
        ll d = bls[i][1];
        while(d > 0) o.pb(min(n , d) - 1) , o.pb(sgn) , o.pb(sgn) , d -= min(n , d);
    }else{
        ll d = bls[i][1];
        while(d > 3) o.pb(min(n + 2 , d) - 3) , o.pb(bls[i][0]) , o.pb(sgn) , d -= min(n + 2 , d);
        for(ll j = 0 ; j < d ; j++) o.pb(bls[i][0]);
    }
}
void f(){
    ord.insert({0 , 0});
    for(ll i = 1 ; i < n ; i++) ord.insert({3 , i}) , infs[i] = 3;
    ll len = (1ll * bls.size());
    for(ll i = 0 ; i < len ; i++){
        ll d = clc(i , 1) - clc(i , 0);
        arr(2) itr = (*ord.find({infs[bls[i][0]] , bls[i][0]}));
        ord.erase(itr);

        infs[bls[i][0]] += d;
        itr[0] += d;
        ord.insert(itr);
        mns[i][0] = (*ord.bg())[1] , mns[i][1] = (*ord.bg())[0];
        if(itr[0] > mns[i][1] + 3){
            ord.erase(itr);
            itr[0] = mns[i][1] + 3;
            infs[bls[i][0]] = mns[i][1] + 3;
            ord.insert(itr);
        }
    }
    ll e = mns[len - 1][0] , sz = mns[len - 1][1];
    for(ll i = len - 1 ; i >= 0 ; i--){
        clcO(i , e);
        if(i == 0){
            if(e != 0) o.pb(0) , o.pb(e) , o.pb(0);
        }else{
            ll d = 0;
            if(bls[i][0] == e) d = clc(i , 1) - clc(i , 0);
            if(mns[i - 1][1] + 3 + d == sz) o.pb(0) , o.pb(e) , o.pb(mns[i - 1][0]) , e = mns[i - 1][0] , sz = mns[i - 1][1];
            else sz -= d;
        }
    }
}

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

    ll e = 0;
    cin >> n >> m;
    for(ll i = 0 ; i < m ;){
        ll d;
        cin >> d;
        if(d != e) add(d , 1) , i++;
        else{
            ll d1 , d2;
            cin >> d1 >> d2;
            if(d1 != e and d2 == 0) e = d1;
            else if(d1 == e) add(e , d2 + 1);
            else add(d1 , d2 + 3);
            i += 3;
        }
    }
    f();
    cout << (1ll * o.size()) << endl;
    for(ll i = (1ll * o.size()) - 1 ; i >= 0 ; i--) cout << o[i] << " ";
}
#Verdict Execution timeMemoryGrader output
Fetching results...