#include <bits/stdc++.h>
#define psb push_back
#define ppb pop_back
#define sz size
#define em empty
#define bgn begin
#define rbgn rbegin
#define fr first
#define sc second
using namespace std;
bool qu(int a, int b){
cout << "? " << a << ' ' << b << endl;
bool ret; cin >> ret; return ret;
}
int main(){
int n; cin >> n;
int ord[n + 1] = {0}; for(int i = 1; i <= n; i++) ord[i] = i;
//even lengths
int ans = 1;
for(int i = 2, len = 1; i <= n; i++){
if(qu(i - 1, i)){
ord[i] = ord[i - 1];
ans = max(ans, ++len);
}else len = 1;
}
//combine
for(int i = 2; i <= n; i++){
if(ord[i - 1] != ord[i]){
int l = i - 1, r = i;
while(r + 1 <= n && ord[r] == ord[r + 1]) r++;
if(r + 1 <= n) r++;
bool y = 1;
while(y){
while((l - 1 > 0 && r + 1 <= n) && ((ord[l - 1] == ord[l] && ord[r] == ord[r + 1]) || r - l + 1 < ans)){
l--;
r++;
}
if(l > 0 && r <= n && qu(l, r)){
ans = max(ans, r - l + 1);
l--; r++;
}
else y = 0;
}
}
}
cout << "! " << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |