# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
129083 | Just_Solve_The_Problem | Mechanical Doll (IOI18_doll) | C++11 | 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 <bits/stdc++.h>
#include "doll.h"
#include "grader.cpp"
using namespace std;
const int maxn = (int)1e6 + 7;
const int inf = (int)1e6 + 6;
int sz;
void solve5(int M, vector<int> A) {
int N = A.size();
vector<int> C(M + 1);
vector<int> X, Y;
C[0] = A[0];
int tmp = N - 1;
while (tmp > 0) {
sz++;
tmp >>= 1;
}
if (sz) C[1] = -1;
else C[1] = 0;
X.resize(sz);
Y.resize(sz);
tmp = N - 1;
for (int i = 1; i <= sz; i++) {
if ((tmp >> (sz - i)) & 1) {
X[i - 1] = A[0];
} else {
X[i - 1] = -1;
}
Y[i - 1] = ((i == sz) ? 0 : -(i + 1));
}
answer(C, X, Y);
}
vector <int> X, Y;
int cnt[maxn];
void add(int val, int v, int l, int r) {
int mid = (l + r) >> 1;
if (cnt[v + v] == cnt[v + v + 1]) {
cnt[v + v + 1]++;
if (mid + 1 == r) {
Y[v + sz - 1] = val;
} else {
add(val, v + v + 1, mid + 1, r);
}
} else {
cnt[v + v]++;
if (l == mid) {
X[v + sz - 1] = val;
} else {
add(val, v + v, l, mid);
}
}
cnt[v]++;
}
void solve1234(int M, vector<int> A) {
int N = A.size();
vector <int> C(M + 1, 0);
vector <int> vec[M + 1];
C[0] = A[0];
if (N == 1) {
answer(C, X, Y);
return ;
}
for (int i = 0; i < N; i++) {
vec[A[i]].push_back((i + 1 == N) ? 0 : A[i + 1]);
}
for (int i = 1; i <= M; i++) {
if (vec[i].empty()) continue;
if (vec[i].size() == 1) {
C[i] = vec[i][0];
continue;
}
int asd = 0;
while ((1 << asd) < vec[i].size()) {
asd++;
}
C[i] = -(sz + 1);
for (int j = 1; j < (1 << asd); j++) {
X.push_back(-(sz + j + j));
Y.push_back(-(sz + j + j + 1));
cnt[j + j] = 0;
cnt[j + j + 1] = 0;
if (j + j >= (1 << asd)) X.back() = -(sz + 1);
if (j + j + 1 >= (1 << asd)) Y.back() = -(sz + 1);
cnt[j] = 0;
}
for (int j = vec[i].size() - 1; j >= 0; j--) {
add(vec[i][j], 1, 0, (1 << asd) - 1);
}
sz += (1 << asd) - 1;
}
//for (int i = 0; i < X.size(); i++) {
//cout << X[i] << ' ' << Y[i] << '\n';
//}
//for (int to : C) {
//cout << to << ' ';
//}
//cout << endl;
answer(C, X, Y);
}
void create_circuit(int M, vector<int> A) {
if (M == 1) {
solve5(M, A);
} else {
solve1234(M, A);
}
}
/*
3 20
1 1 1 1 2 2 2 2 1 1 1 1 2 2 2 2 3 3 3 3
4 2
1 2
3 5
1 2 1 2 2
*/