QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#870948 | #8939. Permutation | thangthang | TL | 0ms | 0kb | C++20 | 1.2kb | 2025-01-25 18:36:07 | 2025-01-25 18:36:17 |
answer
#include <bits/stdc++.h>
using namespace std;
int qry(int l, int r){
cout << "? " << l << ' ' << r << endl;
int ans; cin >> ans;
return ans;
}
int solve(int l, int r, int op){
if (l == r) return l;
if (l + 1 == r) return l ^ r ^ qry(l, r);
if (op == -1){
int mid = qry(l, r);
if (mid == l) return solve(l + 1, r, 0);
if (mid == r) return solve(l, r - 1, 1);
if (mid - l < r - mid){
if (qry(l, mid) == mid) return solve(l, mid - 1, 1);
return solve(mid + 1, r, 0);
}
if (qry(mid, r) == mid) return solve(mid + 1, r, 0);
return solve(l, mid - 1, 1);
}
int len = (r - l + 1) / 3 * 2 - 1;
if (op == 0){
int mid = l + len;
if (qry(l - 1, mid) == l - 1) return solve(l, mid, 0);
return solve(mid + 1, r, -1);
}
int mid = r - len;
if (qry(mid, r + 1) == r + 1) return solve(mid, r, 1);
return solve(l, mid - 1, -1);
}
void process(){
int n; cin >> n;
cout << "! " << solve(1, n, -1) << endl;
}
int main(){
int t; cin >> t; while (t --)
process();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 5
output:
! ? 1 5