Codeforces Contest 730 B Minimum and Maximum —— 交互题

    xiaoxiao2023-11-09  144

    This way

    题意:

    你每次可以问它一个问题,形式如同:? x y表示xy位置哪个值比较大。它会回答你>,=,<三种答案,你要在(n*3+1)/2-2个问题之内找出答案。

    题解:

    用endl的话就不用手动清空缓冲区了 相邻的两个只需要比较一次即可得出最大最小值,但是上面的就需要比较两次,这个比较次数正好是题目给你的次数。

    #include<bits/stdc++.h> using namespace std; #define pa pair<int,int> string s; queue<pa>Q; int main() { int t; cin>>t; while(t--) { int n,x,y; cin>>n; for(int i=1;i<n;i+=2) { cout<<"? "<<i<<" "<<i+1<<endl; cin>>s; if(s[0]=='>'||s[0]=='=') Q.push({i,i+1}); else Q.push({i+1,i}); } if(n%2) Q.push({n,n}); x=Q.front().first,y=Q.front().second; Q.pop(); while(!Q.empty()) { cout<<"? "<<x<<" "<<Q.front().first<<endl; cin>>s; if(s[0]=='<') x=Q.front().first; cout<<"? "<<y<<" "<<Q.front().second<<endl; cin>>s; if(s[0]=='>') y=Q.front().second; Q.pop(); } cout<<"! "<<y<<" "<<x<<endl; } return 0; }
    最新回复(0)