# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
500419 | CatalinT | Unscrambling a Messy Bug (IOI16_messy) | 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 <vector>
#include <iostream>
#include <string>
#include <numeric>
using namespace std;
#include "messy.h"
int W, R;
void rec_write(int l, int r, int n) {
// use sz - 1 ones and 1 0
// use the first half of the range
int sz = r - l + 1;
// cerr << "write: " << sz << " -> " << l << " " << r << "\n";
if (sz == 1) {
return;
}
int m = (l + r) >> 1;
string t = string(n, '0');
for (int i = l; i <= r; i++)
t[i] = '1';
for (int i = l; i <= m; i++) {
auto q = t;
q[i] = '0';
// cerr << q << "\n";
W++;
add_element(q);
}
rec_write(l, m, n);
rec_write(m + 1, r, n);
}
vector<int> sol;
void rec_read(int l, int r, const vector<int>& idx, int n) {
// Range l ... r is known to be in indexes idx
int sz = r - l + 1;
// cerr << "read: " << sz << " -> " << l << " " << r << "\n";
if (sz == 1) {
// At this point we should know the answer for l .. r -> idx[0]
assert(idx.size() == 1);
sol[l] = idx[0];
return;
}
int m = (l + r) >> 1;
//
string t = string(n, '0');
for (int i : idx)
t[i] = '1';
vector<int> left_idx;
vector<int> right_idx;
for (int i : idx) {
auto q = t;
q[i] = '0';
// cerr << q << "\n";
R++;
if (check_element(q)) {
left_idx.push_back(i);
} else {
right_idx.push_back(i);
}
}
rec_read(l, m, left_idx, n);
rec_read(m + 1, r, right_idx, n);
}
std::vector<int> restore_permutation(int n, int w, int r) {
rec_write(0, n - 1, n);
// cerr << "compiled\n";
compile_set();
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sol.assign(n, -1);
rec_read(0, n - 1, idx, n);
// cerr << "W: " << W << " R: " << R << "\n";
// reverse
vector<int> res(n);
for (auto & x : sol)
res[sol[x]] = x;
return res;
}