题解:P9219 「TAOI-1」Antipathy World

· · 题解

前言

一道非常好玩的题

思路

首先观察 k = \lfloor\frac{n}{2}\rfloor + 2,应该是要把整个序列划分成若干个长度为 2 的子序列去询问,最后再询问 2 次。

注意到 a_i \ge 1,再结合一定有一个 a_{max} \ge 2 \times a_i,发现 a_{max} 与另外一个数的差一定严格大于其他任意两个数的差,那么两个两个的询问,找出其中差最大的一组询问,最大值一定是这两个数之一。

我们发现,对于一个 n=3 的序列,我们可以使用 2 次询问获得最大值的位置,那么我们现在已经得知两个数中其中一个是最大值,那我们再随便找一个数加进来,就可以转化为 n=3 的子问题。

## 代码 ```cpp #include<bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while(T--) { int n,k; cin >> n >> k; if(n == 1) { cout << "! " << 1 << endl; } int mx = -1,mxl = 1,mxr = 2,xx; for(int i = 1;i + 1 <= n;i += 2) { cout << "? " << i << " " << i + 1 << endl; cin >> xx; if(xx > mx) { mx = xx,mxl = i,mxr = i + 1; } } int xx1,xx2; cout << "? " << n << " " << mxl << endl; cin >> xx1; cout << "? " << n << " " << mxr << endl; cin >> xx2; if(xx1 > mx && xx2 > mx) { cout << "! " << n << endl; } else if(xx1 > xx2) { cout << "! " << mxl << endl; } else { cout << "! " << mxr << endl; } } return 0; } ```