QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#593325#8939. Permutationlwm7708TL 0ms0kbC++172.7kb2024-09-27 13:19:472024-09-27 13:19:48

Judging History

This is the latest submission verdict.

  • [2024-09-27 13:19:48]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 0kb
  • [2024-09-27 13:19:47]
  • Submitted

answer

#include <algorithm>
#include <cstdint>
#include <ios>
#include <iostream>
#include <ostream>
#include <utility>
#include <vector>

template <typename F>
class y_combinator {

private:

    F f;

public:

    explicit y_combinator(F&& f) : f(f) {}

    template <typename... Args>
    decltype(auto) operator()(Args&&... args) const {

        return f(*this, std::forward<Args>(args)...);

    }

};

template <typename F>
y_combinator(F) -> y_combinator<F>;

void solve() {

    std::int32_t n;

    std::cin >> n;

    std::vector<std::int32_t> dp(n + 1);
    std::vector<std::int32_t> opt(n + 1);
    const auto qry = [](std::int32_t l, std::int32_t r) -> std::int32_t {
        std::cout << "? " << l + 1 << ' ' << r << '\n' << std::flush;
        std::int32_t idx;
        std::cin >> idx;
        --idx;
        return idx;
    };

    dp[1] = -1;

    for (std::int32_t i = 3; i <= n; ++i) {
        std::int32_t& c_opt = opt[i];
        c_opt = opt[i - 1];
        dp[i] = std::max(dp[c_opt] + 1, dp[i - c_opt] + 2);
        while (c_opt + 1 < i) {
            const std::int32_t c_dp = std::max(dp[c_opt + 1] + 1, dp[i - (c_opt + 1)] + 2);
            if (c_dp >= dp[i]) {
                break;
            }
            dp[i] = c_dp;
            ++c_opt;
        }
    }

    y_combinator(
        [&](auto self, std::int32_t idx_l, std::int32_t idx_r, std::int32_t p_idx) -> void {
            const std::int32_t len = idx_r - idx_l;
            if (len == 1) {
                std::cout << "! " << idx_l + 1 << '\n' << std::flush;
                return;
            }
            if (p_idx == -1) {
                p_idx = qry(idx_l, idx_r);
            }
            if (len == 2) {
                std::cout << "! " << (idx_l ^ (idx_l + 1) ^ p_idx) + 1 << '\n' << std::flush;
                return;
            }
            if (p_idx < idx_l + opt[len]) {
                const std::int32_t idx_m = idx_l + opt[len];
                if (qry(idx_l, idx_m) == p_idx) {
                    self(idx_l, idx_m, p_idx);
                } else {
                    self(idx_m, idx_r, -1);
                }
            } else {
                const std::int32_t idx_m = idx_r - opt[len];
                if (qry(idx_m, idx_r) == p_idx) {
                    self(idx_m, idx_r, p_idx);
                } else {
                    self(idx_l, idx_m, -1);
                }
            }
        }
    )(0, n, -1);

}

int main() {

    std::cin.tie(nullptr);

    std::ios_base::sync_with_stdio(false);

    std::int32_t t;

    std::cin >> t;

    for (std::int32_t i = 0; i < t; ++i) {
        solve();
    }

    return 0;

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

3
5
3

output:

? 1 5
? 6 5

result: