QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#599347 | #8939. Permutation | zookeeper | ML | 0ms | 0kb | C++14 | 1.4kb | 2024-09-29 03:37:05 | 2024-09-29 03:37:05 |
Judging History
answer
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int query(int L, int R) {
if (L == R) {
return L;
}
cout << "? " << L << ' ' << R << endl;
int res;
cin >> res;
if (L + 1 == R) {
return res == L ? R : L;
}
while (true) {
if (L == R) {
return L;
}
if (res - L < R - res) {
// Query right
int mid = (res + R) / 2;
cout << "? " << res << ' ' << mid << endl;
int res2;
cin >> res2;
if (res2 == res) {
R = mid;
if (res + 1 == R) {
return R;
}
continue;
}
return query(mid + 1, R);
}
// Query left
int mid = (L + res) / 2;
cout << "? " << mid << ' ' << res << endl;
int res2;
cin >> res2;
if (res2 == res) {
L = mid;
if (L + 1 == res) {
return L;
}
continue;
}
return query(L, mid - 1);
}
}
void solve() {
int N;
cin >> N;
int ans = query(1, N);
cout << "! " << ans << endl;
}
int main() {
fastio;
int T;
cin >> T;
for (;T>0;T--) {
solve();
}
}
詳細信息
Test #1:
score: 0
Memory Limit Exceeded
input:
3 5 3 2
output:
? 1 5 ? 2 3 ! 1