Submission #1244869

#TimeUsernameProblemLanguageResultExecution timeMemory
1244869JoenPoenManMechanical Doll (IOI18_doll)C++20
34 / 100
70 ms11192 KiB
#include <bits/stdc++.h>
#include "doll.h"
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

int makeswitch(vi &outs, vi &x, vi &y, int backloops = -1, int backloopswitch = 0, int switchsize = 0)
{
    if(backloops == switchsize)
        return backloopswitch;

    if(switchsize == 1)
        return outs[0];

    int switchindex = -x.size() - 1;
    x.push_back(0);
    y.push_back(0);

    int size = outs.size();
    if(backloops == -1)
    {
        size = __bit_ceil(outs.size());
        backloops = size - outs.size();
        switchsize = size;
        backloopswitch = switchindex;
    }
    vi a, b;
    int abackloops = min((int)switchsize / 2, backloops), bbackloops = backloops - abackloops;

    queue<int> q;
    for(auto i: outs)
        q.push(i);

    int aloopcount = abackloops, bloopcount = bbackloops;
    for(int i = 0; i < switchsize; i += 2)
    {
        if(aloopcount)
            aloopcount--;
        else
            {
                a.push_back(q.front()); q.pop();
            }
        
        if(bloopcount)
            bloopcount--;
        else
            {
                b.push_back(q.front()); q.pop();
            }
    }

   
    x[-switchindex-1] = makeswitch(a, x, y, abackloops, backloopswitch, switchsize/2);
    y[-switchindex-1] = makeswitch(b, x, y, bbackloops, backloopswitch, switchsize/2);
    return switchindex;
}

void create_circuit(int m, vi a) {
    int n = a.size();
    vvi next(m+1);
    next[0].push_back(a[0]);
    for(int i = 1; i < n; i++)
    {
        next[a[i-1]].push_back(a[i]);/*  */
    }
    next[a[n-1]].push_back(0);

    vi c(m+1), x, y;

    for(int i = 0; i < m + 1; i++)
    {
        if(next[i].empty())
            continue;
        c[i] = makeswitch(next[i], x, y, -1, 0, next[i].size());
    }

    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...