Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
The only value that can appear twice is . So a response is not just “equal”; it is screaming: both endpoints are zeros.
Try querying disjoint pairs. If a pair says , you are done. The hard case is when every queried pair says , meaning each such pair contains at most one zero.
Leave two positions unpaired, say , and pair the rest: . There are pairs and only queries, so this smells intentional.
If all those pair queries return , then positions contain at most zeros. Since there are exactly zeros, at least one of positions is zero.
Use the already-tested pair as a witness. Query and . If either is , answer ; if both are , answer . The count argument forces it.
All nonzero values are unique. Therefore for ,
So if a query returns , both positions are zeros and we can immediately output either endpoint. A return value is weaker: it only says the pair does not contain two zeros.
The adaptive judge detail matters. We are not trying to identify its current array. We are trying to make the transcript force some position to be zero in every valid array. The judge can be slippery, but arithmetic still punches it in the face.
Do not query . Query the disjoint pairs
If any pair returns , output its first endpoint.
Now consider the only annoying case: all these answers are . Let , and let be the number of zeros inside positions . For every queried pair,
So among positions there are at most zeros:
But the whole array contains exactly zeros, hence
Great: at least one of positions and is zero.
We still need to know which of or is guaranteed. We have two queries left, because
Query and, if needed, .
If either query returns , then position equals a zero, so and we output .
If both return , output . Here is the count argument, because this is where the magic happens and also where wrong solutions eat dirt.
Assume for contradiction that . Since at least one of is zero, we must have . Because both and returned , neither nor can be zero. Thus pair contains no zeros. The remaining queried pairs contain at most one zero each, so the total number of zeros is at most
contradicting that there are exactly zeros. Therefore .
The algorithm uses at most
queries per test case, exactly within the limit. The running time is per test case and the memory usage is .
Sources checked: Codeforces statement, official contest editorial.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
if (!(cin >> T)) return 0;
auto ask = [](int i, int j) -> int {
cout << '?' << ' ' << i << ' ' << j << endl;
int res;
if (!(cin >> res)) exit(0);
if (res == -1) exit(0);
return res;
};
auto answer = [](int k) {
cout << '!' << ' ' << k << endl;
};
while (T--) {
int n;
if (!(cin >> n)) return 0;
if (n == -1) return 0;
int ans = -1;
for (int i = 1; i < n; ++i) {
int x = 2 * i + 1;
int y = 2 * i + 2;
if (ask(x, y) == 1) {
ans = x;
break;
}
}
if (ans == -1) {
if (ask(1, 3) == 1) {
ans = 1;
} else if (ask(1, 4) == 1) {
ans = 1;
} else {
ans = 2;
}
}
answer(ans);
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
if (!(cin >> T)) return 0;
auto ask = [](int i, int j) -> int {
cout << '?' << ' ' << i << ' ' << j << endl;
int res;
if (!(cin >> res)) exit(0);
if (res == -1) exit(0);
return res;
};
auto answer = [](int k) {
cout << '!' << ' ' << k << endl;
};
while (T--) {
int n;
if (!(cin >> n)) return 0;
if (n == -1) return 0;
int ans = -1;
for (int i = 1; i < n; ++i) {
int x = 2 * i + 1;
int y = 2 * i + 2;
if (ask(x, y) == 1) {
ans = x;
break;
}
}
if (ans == -1) {
if (ask(1, 3) == 1) {
ans = 1;
} else if (ask(1, 4) == 1) {
ans = 1;
} else {
ans = 2;
}
}
answer(ans);
}
return 0;
}