QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#599373#8939. PermutationzookeeperTL 0ms0kbC++141.6kb2024-09-29 03:54:462024-09-29 03:54:47

Judging History

This is the latest submission verdict.

  • [2024-09-29 03:54:47]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 0kb
  • [2024-09-29 03:54:46]
  • Submitted

answer

#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

int solve() {
    int N;
    cin >> N;
    int L = 1, R = N, S = -1;
    while (true) {
        if (L == R) return L;
        if (S == -1) {
            cout << "? " << L << ' ' << R << endl;
            cin >> S;
            continue;
        }
        if (L + 1 == R) {
            return S == L ? R : L;
        }
        if (S - L < R - S) {
            // query right
            int mid = (S + R + 1) / 2;
            cout << "? " << S << ' ' << mid << endl;
            int SS;
            cin >> SS;
            if (SS == S) {
                L = S;
                R = mid;
                if (L + 1 == R) {
                    return R;
                }
                continue;
            }
            L = mid + 1;
            S = -1;
            continue;
        } else {
            // query left
            int mid = (L + S) / 2;
            cout << "? " << mid << ' ' << S << endl;
            int SS;
            cin >> SS;
            if (SS == S) {
                R = S;
                L = mid;
                if (L + 1 == R) {
                    return L;
                }
                continue;
            }
            R = mid - 1;
            S = -1;
        }
    }





    // int ans = query(1, N);
    // cout << "! " << ans << endl;
}

int main() {
    fastio;
    int T;
    cin >> T;
    for (;T>0;T--) {
        int s = solve();
        cout << "! " << s << endl;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

3
5
3
2

output:

? 1 5
? 2 3
! 1

result: