logo AlgoBeat OnlineJudge
登录 注册

题解

作者: _ZXY_  ·  发布于 2026-07-30 11:19:27  ·  最后修改于 2026-07-30 11:27:56
已通过
审核员:AlgoBeat 官方账号 · 2026-07-30 11:27:56

考虑二分区间。我们实现一个分治函数 ,每次判断是否在 里,递归即可。
由于最多询问 次,所以可以在 次询问内得出答案。

#include<bits/stdc++.h>
#define int long long 
using namespace std;
int n;
int sol(int l,int r){
	if(l==r)return l;
	int mid=l+r>>1;
	cout<<"? "<<l<<' '<<mid<<endl;
	int x;
	cin>>x;
	if(x)return sol(l,mid);
	else return sol(mid+1,r);
}
void solve(){
   ios::sync_with_stdio(0);
   cin.tie(0);cout.tie(0);
   cin>>n;
   int ans=sol(1,n);
   cout<<"! "<<ans<<endl;
}
signed main(){
   solve();
}//ZXY AK IOI!

暂无评论

登录 后即可评论。