题解:CF2022D1 Asesino (Easy Version)
思路
手玩样例就可以发现假设 ? a b 的询问的答案为
那么我们就可以将
接着我们就只要判断这一组里具体哪个是内鬼了。
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e5+25;
const int inf=2e9;
int n;
inline void solve(){
cin>>n;
pair<int,int>sub;
if(n&1){
cout<<"? "<<1<<' '<<n<<endl;
int res;
cin>>res;
int ans=res;
cout<<"? "<<n<<' '<<1<<endl;
cin>>res;
if(ans!=res)sub=make_pair((long long)1,n);
for(int i=1;i<=n-1;i+=2){
int x,y;
cout<<"? "<<i<<' '<<i+1<<endl;
cin>>x;
cout<<"? "<<i+1<<' '<<i<<endl;
cin>>y;
if(x!=y){sub=make_pair(i,i+1);break;}
}
int x=sub.first,y=sub.second;
int z=1;
while(1){
z=rand()%n+1;
if(z!=x&&z!=y)break;
}
int p1,p2,p3,p4;
cout<<"? "<<x<<' '<<z<<endl;
cin>>p1;
cout<<"? "<<z<<' '<<x<<endl;
cin>>p2;
if(p1!=p2){cout<<"! "<<x<<endl;return ;}else cout<<"! "<<y<<endl;
}
//这里分讨了 n 为奇数和偶数
else{
for(int i=1;i<=n-1;i+=2){
int x,y;
cout<<"? "<<i<<' '<<i+1<<endl;
cin>>x;
cout<<"? "<<i+1<<' '<<i<<endl;
cin>>y;
if(x!=y){sub=make_pair(i,i+1);break;}
}
int x=sub.first,y=sub.second;
int z=1;
while(1){
z=rand()%n+1;
if(z!=x&&z!=y)break;
}
int p1,p2,p3,p4;
cout<<"? "<<x<<' '<<z<<endl;
cin>>p1;
cout<<"? "<<z<<' '<<x<<endl;
cin>>p2;
if(p1!=p2){cout<<"! "<<x<<endl;return ;}else cout<<"! "<<y<<endl;
}
}
signed main(){
srand(time(0));
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int T;
cin>>T;
while(T--)solve();
return 0;
}