Submission #1310741

#TimeUsernameProblemLanguageResultExecution timeMemory
1310741theiuliusCave (IOI13_cave)C++20
0 / 100
219 ms616 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#include "cave.h"

void exploreCave(int N) {
    int n = N;
    vector<int> used(n, 0);   // which switches already fixed (mp in your code)
    int a[n] = {}, b[n] = {};
    for (int i = 0; i < n; i++){
      b[i] = -1;
    }

    for (int i = 0; i < n; ++i) {
        // set all unfixed switches to 0 and query to determine berk
        for (int j = 0; j < n; ++j) if (!used[j]) a[j] = 0;
        int x = tryCombination(a);
        int berk = (x > i || x == -1) ? 0 : 1;

        // build list of unused switches (indices)
        vector<int> unused;
        unused.reserve(n);
        for (int j = 0; j < n; ++j) if (!used[j]) unused.push_back(j);

        // binary search on positions inside `unused`
        int L = 0, R = (int)unused.size() - 1;
        int pos = -1;
        while (L <= R) {
            int mid = (L + R) >> 1;

            // set first mid+1 unused switches to berk, rest to 1^berk
            for (int k = 0; k <= mid; ++k) a[unused[k]] = berk;
            for (int k = mid + 1; k < (int)unused.size(); ++k) a[unused[k]] = (1 ^ berk);

            x = tryCombination(a);
            if (x > i || x == -1) {
                pos = mid;      // possible answer in unused[pos]
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }

        // pos must be valid; but just in case (defensive), handle -1
        if (pos == -1) pos = (int)unused.size() - 1;

        int ans = unused[pos];
        b[ans] = i;
        used[ans] = 1;
        a[ans] = berk;    // fix this switch permanently
    }

    answer(a, b);
}
#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...