Submission #1336618

#TimeUsernameProblemLanguageResultExecution timeMemory
1336618sh_qaxxorov_571Library (JOI18_library)C++20
100 / 100
113 ms424 KiB
#include "library.h"
#include <vector>
#include <algorithm>

using namespace std;

void Solve(int N) {
    if (N == 1) {
        Answer({1});
        return;
    }

    vector<int> res;
    vector<bool> used(N + 1, false);

    // Birinchi chetki kitobni topish (ixtiyoriy bittasini boshlash uchun)
    int current = 1;
    for (int i = 1; i <= N; i++) {
        vector<int> M(N, 1);
        M[i - 1] = 0;
        if (Query(M) == 1) { // Agar bir kitobni olib tashlaganda ham 1 blok qolsa, u chetki kitob
            current = i;
            break;
        }
    }

    res.push_back(current);
    used[current] = true;

    for (int i = 1; i < N; i++) {
        vector<int> candidates;
        for (int j = 1; j <= N; j++) {
            if (!used[j]) candidates.push_back(j);
        }

        // Ikkilik qidiruv (Binary Search) orqali keyingi qo'shni kitobni topish
        int low = 0, high = (int)candidates.size() - 1;
        int next_book = candidates[0];

        while (low <= high) {
            int mid = low + (high - low) / 2;
            vector<int> M(N, 0);
            for (int k = 0; k <= mid; k++) M[candidates[k] - 1] = 1;
            
            int q1 = Query(M);
            M[current - 1] = 1;
            int q2 = Query(M);

            if (q1 >= q2) { // current kitobi bu oraliqdagi kitoblardan biriga qo'shni
                next_book = candidates[mid];
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }

        current = next_book;
        res.push_back(current);
        used[current] = true;
    }

    Answer(res);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...