CF1807E Interview

· · 题解

一道二分板子题。

对于当前区间 [l,r],先设 [l,r] 的中点为 mid,然后提问 [l,mid] 的重量和。如果返回的重量和不等于数量和,则 m 一定在 [l,mid] 区间内,所以将 r 缩到 mid-1;否则,m 就将 l 扩大到 mid+1。在此期间,不断将答案赋值为 mid 就可以了。

如果你的代码正确,但是没有通过,(或者在原网页上显示 Idleness\space Limit\space Exceeded)建议使用 C++ 的 cin 和 cout,并使用 endl 作为换行。

```cpp #include <iostream> #include <cstdio> using namespace std; const int maxn = 2e5 + 5; int t, n; int a[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while(t--) { cin >> n; for(int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i-1]; } int l = 1, r = n, ans = 1, p; while(l <= r) { int mid = (l + r) >> 1; cout << '\?' << " " << (mid-l+1); for(int i = l; i <= mid; i++) cout << " " << i; cout << endl; cin >> p; ans = mid; if(p == a[mid]-a[l-1]) l = mid + 1; else r = mid - 1; } cout << "! " << l << endl; } return 0; } ```