#include "cave.h"
#include <vector>
using namespace std;
int N;
vector<int> doorOfSwitch; // to what door is switch linked, -1 if not determined
vector<int> switchOfDoor; // to what switch is door linked, -1 if not determined
vector<int> mustBeDown; // i-th switch must be down to open corresponding door, false if not determined
void exploreCave(int n)
{
N = n;
doorOfSwitch = vector<int>(N, -1);
switchOfDoor = vector<int>(N, -1);
mustBeDown = vector<int>(N, 0);
// iterating over doors, determining corresponding switches one by one
for (int i = 0; i < N; ++i)
{
// determining is corresponding door switch must be down
int resSwitchIsUp = tryCombination(mustBeDown.data());
int switchMustBeDown = !(resSwitchIsUp == -1 || resSwitchIsUp > i + 1);
// filling a query with known data
vector<int> query(N);
for (int j = 0; j < i; ++j) // doors for which we know all info
{
query[switchOfDoor[j]] = mustBeDown[switchOfDoor[j]];
}
// now determining which exactly switch is corresponding to i-th door
int l = 0;
int r = N - 1;
int switchPos = N - 1;
while (l <= r)
{
int mid = (l + r) / 2;
for (int j = 0; j <= mid; ++j) // here filling switches with current good, where doorOfSwitch[j] == -1
{
if (doorOfSwitch[j] == -1)
{
query[j] = switchMustBeDown;
}
}
for (int j = mid + 1; j < N; ++j) // here filling switches with current bad, where doorOfSwitch[j] == -1
{
if (doorOfSwitch[j] == -1)
{
query[j] = !switchMustBeDown;
}
}
int queryRes = tryCombination(query.data());
if (queryRes == -1 || queryRes > i + 1)
{
r = mid - 1;
switchPos = mid;
}
else
{
l = mid + 1;
}
}
mustBeDown[switchPos] = switchMustBeDown;
doorOfSwitch[switchPos] = i;
switchOfDoor[i] = switchPos;
}
answer(doorOfSwitch.data(), mustBeDown.data());
}
# | 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... |