题解 AT_abc466_c [ABC466C] Count Close Pairs
题意
数轴上有
每次可以询问 ? i j,得到点
询问次数不能超过
思路
由于点从左到右排列,如果我们固定左端点 No,再往后的点与
也就是说,对于每个
因此,我们可以用双指针来询问。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, r = 1;
long long ans = 0;
cin >> n;
for (int l = 1; l <= n; l++) {
if (r < l) r = l;
while (r < n) {
cout << "? " << l << " " << r + 1 << endl;
string s;
cin >> s;
if (s == "Yes") {
r++;
} else {
break;
}
}
ans += r - l;
}
cout << "! " << ans;
return 0;
}