Submission #1274189

#TimeUsernameProblemLanguageResultExecution timeMemory
1274189RomanLeshchukCave (IOI13_cave)C++20
100 / 100
485 ms532 KiB
#include "cave.h"

#include <vector>
#include <iostream>

using namespace std;

static int N;
static vector<int> doorOfSwitch; // to what door is switch linked, -1 if not determined
static vector<int> switchOfDoor; // to what switch is door linked, -1 if not determined
static 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)
    {
        // making all unknown up
        for (int j = 0; j < N; ++j) 
        {
            if (doorOfSwitch[j] == -1)
            {
                mustBeDown[j] = 0;
            }
        }

        // determining is corresponding door switch must be down
        // switches linked to prev doors are correct, other are up
        // if must be down, then it will stuck exactly on i-th door
        int switchMustBeDown = tryCombination(mustBeDown.data()) == i;

        // now determining which exactly switch is linked with 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, and only linked to next doors
            {
                if (doorOfSwitch[j] == -1)
                {
                    mustBeDown[j] = switchMustBeDown;
                }
            }
            for (int j = mid + 1; j < N; ++j) // here filling switches with current bad, and only linked to next doors
            {
                if (doorOfSwitch[j] == -1)
                {
                    mustBeDown[j] = !switchMustBeDown;
                }
            }

            int queryRes = tryCombination(mustBeDown.data());
            if (queryRes == i)
            {
                // stuck, must search further
                l = mid + 1;
            }
            else
            {
                // good, must narrow search
                r = mid - 1;
                switchPos = mid;
            }
        }

        mustBeDown[switchPos] = switchMustBeDown;
        doorOfSwitch[switchPos] = i;
        switchOfDoor[i] = switchPos;
    }

    answer(mustBeDown.data(), doorOfSwitch.data());
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...