# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
221638 | galca | Mechanical Doll (IOI18_doll) | C++14 | 2 ms | 204 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "doll.h"
#include <vector>
#include <iostream>
using namespace std;
// N + 1 = 15
// num_bits = 4
// we should have: 1 + 2 + 4 + 8
void create_circuit(int M, std::vector<int> A) {
int N = A.size();
std::vector<int> C(M + 1);
for (int i = 0; i <= M; ++i) {
C[i] = -1;
}
std::vector<int> X, Y;
// compute depth and number of elements in each tree + remaining counter
int num_bits = 0;
int tmp = N - 1;
while (tmp > 0) {
++num_bits;
tmp >>= 1;
}
vector<int> trees;
int current_tree = 1 << num_bits; // for N = 16, this is 16
int sum_of_trees = 0;
while (sum_of_trees < N) {
trees.push_back(current_tree);
sum_of_trees += current_tree;
current_tree >>= 1;
}
int tail_counter = trees[trees.size() - 1];
// create the trees
// all triggers belong to the trees
// the counter eventually lead to exit
int idx = 1;
for (int i = 0; i < trees.size(); i++) {
int current_size = trees[i];
// add the root
X.push_back(-(idx+1));
Y.push_back(-(idx + current_size));
// the tree itself
int my_idx = 1;
int start_idx = idx;
for (int k = 1; (1 << (k - 1)) < current_size; ++k) {
int n_nodes = 1 << (k - 1);
//cout << "creating " << n_nodes << " for the tree of " << current_size << endl;
for (int n = 1; n <= n_nodes; n++) {
X.push_back(-(start_idx + my_idx * 2));
Y.push_back(-(start_idx + my_idx * 2 + 1));
my_idx++;
}
}
//cout << "overall in the tree of " << current_size << " created " << my_idx << " nodes " << endl;
// replace last level with triggers
int n_nodes = current_size >> 1;
int idx_b = X.size() - n_nodes;
for (int n = 0; n < current_size; n++) {
// revert the number
int offset = n_nodes;
int n_tmp = n;
int n_pos = 0;
while (n_tmp > 0) {
if (n_tmp & 1) {
n_pos += offset;
}
n_tmp >>= 1;
offset >>= 1;
}
if (n_pos & 1) {
Y[idx_b + n_pos / 2] = A[n];
//cout << "connecting trigger " << n << " to Y at " << n_pos / 2 << endl;
}
else {
X[idx_b + n_pos / 2] = A[n];
//cout << "connecting trigger " << n << " to X at " << n_pos / 2 << endl;
}
}
idx += current_size;
}
#if 0
cout << "arrays so far " << endl;
for (int i = 0; i < X.size(); i++) {
cout << X[i] << " ";
}
cout << endl;
for (int i = 0; i < Y.size(); i++) {
cout << Y[i] << " ";
}
cout << endl;
#endif
// add the counter
tail_counter >>= 1;
while (tail_counter > 0) {
X.push_back(-1);
Y.push_back(-(idx+1));
++idx;
tail_counter >>= 1;
}
Y[Y.size() - 1] = 0;
#if 0
cout << "arrays with the tail counter " << endl;
for (int i = 0; i < X.size(); i++) {
cout << X[i] << " ";
}
cout << endl;
for (int i = 0; i < Y.size(); i++) {
cout << Y[i] << " ";
}
cout << endl;
#endif
#if 0
for (int n = 0; n < N; n++) {
// revert the number
int offset = n_leaves;
int n_tmp = n;
int n_pos = 0;
while (n_tmp > 0) {
if (n_tmp & 1) {
n_pos += offset;
}
n_tmp >>= 1;
offset >>= 1;
}
if (n_pos & 1) {
Y[idx_b + n_pos/2] = A[n];
//cout << "connecting trigger " << n << " to Y at " << n_pos / 2 << endl;
}
else {
X[idx_b + n_pos/2] = A[n];
//cout << "connecting trigger " << n << " to X at " << n_pos / 2 << endl;
}
}
#endif
answer(C, X, Y);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |