# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
219441 | IgorI | Mechanical Doll (IOI18_doll) | C++17 | 0 ms | 0 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 <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
/*void answer(vector<int> c, vector<int> x, vector<int> y)
{
//cout << "final : " << c.size() << " " << x.size() << " " << y.size() << endl;
for (int i = 0; i < c.size(); i++) cout << c[i] << "\n";
for (int i = 0; i < x.size(); i++) cout << x[i] << " " << y[i] << "\n";
exit(0);
}*/
vector<int> c, x, y;
vector<int> bitreverse(int k)
{
vector<int> c;
for (int i = 0; i < (1 << k); i++)
{
int y = 0;
for (int j = 0; j < k; j++)
{
y = 2 * y + (((1 << j) & i) > 0);
}
c.push_back(y);
}
return c;
}
void build(int f, vector<int> v)
{
//cout << f << " -> ";
//for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
//cout << endl;
if (v.size() == 1)
{
c[f] = v[0];
return;
}
for (int i = 1; i < 20; i++)
{
if (v.size() <= (1 << i))
{
vector<int> tree = {f};
while (tree.size() < (1 << i))
{
x.push_back(0);
y.push_back(0);
tree.push_back(-x.size());
}
while (tree.size() < 2 * (1 << i))
{
tree.push_back(tree[1]);
}
vector<int> reach = bitreverse(i);
int free = (1 << i) - v.size();
for (int j = 0; j < reach.size(); j++)
{
if (reach[j] >= free)
{
tree[(1 << i) + j] = v[reach[j] - free];
}
}
c[tree[0]] = tree[1];
for (int i = 1; 2 * i + 1 < tree.size(); i++)
{
x[-tree[i] - 1] = tree[2 * i];
y[-tree[i] - 1] = tree[2 * i + 1];
}
break;
}
}
}
void create_circuit(int m, vector<int> a)
{
c.resize(m + 1);
vector<vector<int> > go(m + 1);
a.push_back(0);
go[0].push_back(a[0]);
for (int i = 0; i + 1 < a.size(); i++)
{
go[a[i]].push_back(a[i + 1]);
}
for (int i = 0; i < m + 1; i++)
{
if (go[i].size() == 0) go[i] = {0};
}
for (int i = 0; i < m + 1; i++)
{
build(i, go[i]);
}
answer(c, x, y);
}
/*int main()
{
int m, n;
cin >> m >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
create_circuit(m, a);
}*/