Submission #817011

#TimeUsernameProblemLanguageResultExecution timeMemory
817011Jarif_RahmanMechanical Doll (IOI18_doll)C++17
100 / 100
83 ms13856 KiB
#include "doll.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

const int inf = 1e7;

void subtask_1_2_3(int m, vector<int> A){
    int n = A.size();
    A.pb(inf);
    vector<vector<int>> pos(m);
    for(int i = 0; i < n; i++) pos[A[i]-1].pb(i);

    vector<int> C(m+1, 0), X, Y;
    C[0] = A[0];
    int ls = 0;
    for(int i = 0; i < m; i++) if(!pos[i].empty()){
        if(pos[i].size() == 1){
            C[i+1] = A[pos[i][0]+1];
        }
        else if(pos[i].size() == 2){
            X.pb(A[pos[i][0]+1]), Y.pb(A[pos[i][1]+1]);
            C[i+1] = -int(X.size());
        }
        else if(pos[i].size() == 4){
            C[i+1] = -int(X.size()+1);
            X.pb(-int(X.size()+2)), Y.pb(-int(Y.size()+3));
            X.pb(A[pos[i][0]+1]), X.pb(A[pos[i][1]+1]);
            Y.pb(A[pos[i][2]+1]), Y.pb(A[pos[i][3]+1]);
        }
        else{
            C[i+1] = -int(X.size()+1);
            X.pb(-int(X.size()+2)), Y.pb(-int(Y.size()+3));
            X.pb(A[pos[i][0]+1]), X.pb(A[pos[i][1]+1]);
            Y.pb(A[pos[i][2]+1]), Y.pb(ls);
            ls = -int(X.size())+2;
        }
    }

    for(int &x: C) if(x == inf) x = ls;
    for(int &x: X) if(x == inf) x = ls;
    for(int &x: Y) if(x == inf) x = ls;

    answer(C, X, Y);
}

void create_circuit(int m, vector<int> A){
    int n = A.size();

    vector<int> cnt(m, 0);
    for(int &x: A) cnt[x-1]++;
    if(*max_element(cnt.begin(), cnt.end()) <= 4){
        subtask_1_2_3(m, A);
        return;
    }

    int k = 1;
    while(k < n) k*=2;

    vector<int> sth(2*k, -2);
    vector<pair<int, int>> candidates;
    for(int i = k; i < 2*k; i++){
        int j = i, pos = 0;
        while(j != 1){
            if(j&1) pos++;
            pos<<=1;
            j>>=1;
        }
        pos>>=1;
        if(2*k-i-1 < n) candidates.pb({pos, i});
        sth[i] = -1;
    }

    sort(candidates.begin(), candidates.end());
    for(int i = 1; i < n; i++) sth[candidates[i-1].sc] = A[i];
    sth[candidates.back().sc] = 0;

    vector<int> C(m+1, -1), X, Y;
    C[0] = A[0];

    for(int i = k-1; i >= 1; i--){
        if(sth[2*i] == -1 && sth[2*i+1] == -1) sth[i] = -1;
    }

    function<int(int)> rec = [&](int i){
        X.pb(0), Y.pb(0);
        int j = X.size();

        if(sth[2*i] == -1) X[j-1] = -1;
        else if(sth[2*i] >= 0) X[j-1] = sth[2*i];
        else X[j-1] = rec(2*i);

        if(sth[2*i+1] == -1) Y[j-1] = -1;
        else if(sth[2*i+1] >= 0) Y[j-1] = sth[2*i+1];
        else Y[j-1] = rec(2*i+1);

        return -j;
    };

    rec(1);

    answer(C, X, Y);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...