# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
784162 | Boas | Cave (IOI13_cave) | 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 <bits/stdc++.h>
#include "cave.h"
#include "grader.c"
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define set(x, v) fill(ALL(x), v)
typedef vector<int> vi;
void exploreCave(int N)
{
vi switches(N, 0);
vi revS(N, 0); // the correct position of each switch corresponding to door i
vi D(N, -1); // element D[i] should contain the door number that switch i is connected to
vi revD(N, -1); // element revD[i] should contain the switch number that door i is connected to
bool done = false;
for (int i = 0; i < N; i++)
{
if (i > 0)
set(switches, 0);
for (int j = 0; j < N; j++)
{
if (revD[j] == -1)
break;
switches[revD[j]] = revS[j];
}
revS[i] = tryCombination(switches.data()) == i;
int min = 0, max = N - 1;
while (max - min > 0)
{
int s = (max + min) / 2;
set(switches, !revS[i]);
fill(switches.begin() + min, switches.begin() + s + 1, revS[i]);
for (int j = 0; j < N; j++)
{
if (revD[j] == -1)
break;
switches[revD[j]] = revS[j];
}
int d = tryCombination(switches.data());
if (d == -1)
{
done = true;
break;
}
if (d > i)
{
if (s == max)
throw;
max = s;
}
else
{
min = s + 1;
}
}
if (done)
break;
revD[i] = min;
D[min] = i;
}
for (int i = 0; i < N; i++)
{
if (D[i] != -1)
continue;
switches[i] = !switches[i];
int d = tryCombination(switches.data());
D[i] = d;
switches[i] = !switches[i];
}
answer(switches.data(), D.data());
}