Submission #1244881

#TimeUsernameProblemLanguageResultExecution timeMemory
1244881j_vdd16Mechanical Doll (IOI18_doll)C++20
47 / 100
659 ms9688 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <limits.h>

#include "doll.h"

using namespace std;

#define loop(X, N) for (int X = 0; X < (N); X++)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;

int numBits = 0;
int rev(int n) {
    int out = 0;
    for (int i = 0; i < numBits; i++) {
        if (n & (1 << i))
            out |= 1 << (numBits - 1 - i);
    }

    return out;
}

bool cmp(int a, int b) {
    return rev(a) < rev(b);
}

void create_circuit(int m, vi a) {
    vi c; //exits
    vi x, y;

    int n = a.size();
    c.push_back(a[0]);
    
    int pow = 1;
    while (pow < n) {
        numBits++;
        pow *= 2;
    }

    vi order(pow);
    for (int i = 0; i < pow; i++) {
        order[i] = i;
    }

    sort(order.begin(), order.end(), cmp);

    if (pow == 1) {
        c.push_back(0);
    }
    else {
        vi exits(pow * 2);
        for (int j = 0; j < n - 1; j++) {
            exits[order[j] + pow] = a[j + 1];
        }
        for (int j = n - 1; j < pow - 1; j++) {
            exits[order[j] + pow] = INT_MAX;
        }
        exits[order[pow - 1] + pow] = 0;

        int nodes = 1;
        for (int j = pow - 1; j >= 1; j--) {
            if (exits[j * 2] == INT_MAX && exits[j * 2 + 1] == INT_MAX) {
                exits[j] = INT_MAX;
            }
            else {
                x.push_back(exits[j * 2]);
                y.push_back(exits[j * 2 + 1]);
                exits[j] = -(nodes++);
            }
        }

        for (int& idx : c) if (idx == INT_MAX) idx = -(nodes - 1);
        for (int& idx : x) if (idx == INT_MAX) idx = -(nodes - 1);
        for (int& idx : y) if (idx == INT_MAX) idx = -(nodes - 1);

        for (int i = 0; i < m; i++)
            c.push_back(-(nodes - 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...