Submission #587051

#TimeUsernameProblemLanguageResultExecution timeMemory
587051kamelfanger83Mechanical Doll (IOI18_doll)C++14
0 / 100
1 ms300 KiB
#include <iostream>
#include <vector>
#include "doll.h"
#include <functional>
#include <cassert>

using namespace std;

/*void answer(vector<int> C, vector<int> X, vector<int> Y){
    return;
}*/

void create_circuit(int M, vector<int> A) {
    int N = A.size();
    int cs = 1;
    vector<int> C(M + 1);
    vector<int> X (2*N, 0), Y(2*N, 0);
    C[0] = A[0];
    auto ghbit = [&](int n){
        int hbit = 0;
        for(int bit = 0; bit < 30; bit++) if((n & (1<<bit)) != 0) hbit = bit;
        return hbit;
    };
    auto buildfull = [&](auto&& self, int height){
        int root = cs;
        cs++;
        if(height == 1) return;
        X[root] = -cs;
        self(self, height-1);
        Y[root] = -cs;
        self(self, height-1);
    };
    function<void(int, int)> buildpartial = [&](int n, int height)->void{
        int root = cs;
        cs++;
        if(height == 1) return;
        if(n >= 1<<(height-1)){
            X[root] = -cs;
            buildfull(buildfull, height-1);
            if(n - (1<<(height-1)) > 0){
                Y[root] = -cs;
                buildpartial(n - (1<<(height-1)), height-1);
            }
            else Y[root] = -1;
        }
        else{
            X[root] = -cs;
            buildpartial(n, height-1);
            Y[root] = -1;
        }
        cs++;
    };
    buildpartial(N, ghbit(N)+1);
    for(int rooter = 1; rooter <= M; rooter++) C[rooter] = -1;
    int ca = 1;
    vector<bool> sparity (2*N, 0);
    int c = A[0];
    while(true){
        if(c >= 0) c = C[c];
        else{
            int oc = c;
            if(sparity[-c] == false){
                if(X[-c] == 0){
                    if(ca == N){
                        assert(false);
                    }
                    X[-c] = A[ca++];
                }
                c = X[-c];
            }
            else if(sparity[-c] == true){
                if(Y[-c] == 0){
                    if(ca == N){
                        Y[-c] = 0;
                        goto ans;
                    }
                    Y[-c] = A[ca++];
                }
                c = Y[-c];
            }
            sparity[-oc] = !sparity[-oc];
        }
    }
    ans:
    answer(C, X, Y);
}

/*int main(){
    vector<int> A = {1,2,3,4,5,6,7,8,9,10};
    create_circuit(5, A);
}*/
#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...