Submission #925002

# Submission time Handle Problem Language Result Execution time Memory
925002 2024-02-10T12:29:29 Z parlimoos RLE (BOI06_rle) C++14
95 / 100
1656 ms 82236 KB
//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'

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

void add(int c , int x){
    int len = (int)bls.size();
    if(!bls.empty() and bls[len - 1][0] == c) bls[len - 1][1] += (1ll * x);
    else bls.pb({c , x});
}
int clc(int i , bool sgn){
    if(sgn) return (3 * ((bls[i][1] + n - 1) / n));
    int 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(int i , int sgn){
    if(sgn == bls[i][0]){
        int d = bls[i][1];
        while(d > 0) o.pb(min(n , d) - 1) , o.pb(sgn) , o.pb(sgn) , d -= min(n , d);
    }else{
        int 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(int j = 0 ; j < d ; j++) o.pb(bls[i][0]);
    }
}
void f(){
    ord.insert({0 , 0});
    for(int i = 1 ; i < n ; i++) ord.insert({3 , i}) , infs[i] = 3;
    int len = (int)bls.size();
    for(int i = 0 ; i < len ; i++){
        int 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);
        }
    }
    int e = mns[len - 1][0] , sz = mns[len - 1][1];
    for(int 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{
            int 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);

    int e = 0;
    cin >> n >> m;
    for(int i = 0 ; i < m ;){
        int d;
        cin >> d;
        if(d != e) add(d , 1) , i++;
        else{
            int d1 , d2;
            cin >> d1 >> d2;
            if(d1 != e and d2 == 0) e = d1;
            else if(d1 == e) add(e , d2 + 1);
            else if(d1 != e and d2 > 0) add(d1 , d2 + 3);
            i += 3;
        }
    }
    f();
    cout << (int)o.size() << endl;
    for(int i = (int)o.size() - 1 ; i >= 0 ; i--) cout << o[i] << " ";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 13 ms 1372 KB Output is correct
7 Correct 95 ms 9296 KB Output is correct
8 Correct 123 ms 12404 KB Output is correct
9 Correct 241 ms 36280 KB Output is correct
10 Correct 90 ms 8776 KB Output is correct
11 Correct 81 ms 8572 KB Output is correct
12 Correct 251 ms 26940 KB Output is correct
13 Correct 231 ms 17748 KB Output is correct
14 Correct 136 ms 11892 KB Output is correct
15 Correct 74 ms 7396 KB Output is correct
16 Correct 238 ms 16988 KB Output is correct
17 Correct 320 ms 22840 KB Output is correct
18 Incorrect 342 ms 21824 KB the output code does not encode the input sequence
19 Correct 1568 ms 82236 KB Output is correct
20 Correct 1656 ms 81856 KB Output is correct