题解:P16126 [USTCPC 2026] Note Offset Adjustment
思路分析
一看,像是一种二分的题目,但是不好直接二分:区间太长了!
与是一个很自然的想法是先将答案限制在一个长度可以接受的范围之内,然后在考虑怎么二分。
巧了!一次查询可以确定一个数字在不在某一个长度为
考虑一个最基础的二分:对于区间
但是,你发现这样做总共最多需要
欸!注意到区间长度最长不超过 Good,那么说明答案在 Perfect,那么说明在
我们做到了单次将区间长度砍到原区间的
不过上面的方法已经启发了我们:对于第一次分治得到的区间,我们再查询一下
我们对于这种分治执行两次,此时不难推出来剩余区间的长度最长为
此题可解,考场代码如下:
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
int t, l, r, mid;
string s[12]; int a[12];
int ans, cnt;
inline string query(int p) {
cerr << "[" << l << "," << r << "]: Querying " << p << endl;
cnt++;
if (abs(p - ans) <= 80) return "Perfect";
if (abs(p - ans) <= 160) return "Good";
if (abs(p - ans) <= 180) return "Bad";
return "Miss";
}
inline int search() {
while (l != r) {
mid = l + r + 1 >> 1;
a[0] = mid + 180;
#ifndef _MSC_VER
cout << "? " << a[0] << endl;
cin >> s[0];
#else
s[0] = query(a[0]);
#endif
if (s[0] == "Miss") r = mid - 1;
else l = mid;
}
return l;
}
inline void verify(int p) {
if (p != ans) cerr << p << " " << ans << endl, exit(1);
else if (cnt > 10) cerr << p << endl, exit(2);
else cout << "Pass\n";
}
int main() {
ios::sync_with_stdio(0);
srand((unsigned)time(0));
for (cin >> t;t;t--) {
/*
首先通过两次询问,我们可以将答案限制在一个长度为 360 的区间内。
具体来说,我们选择两个查询点,
一个的 Bad 区间正好包含左
一个的 Bad 区间正好包含右
中间没有覆盖到的部分长度是不超过 360 的。
接下来,有一种基础的二分:
假设区间在 [l,r],中点为 mid
那么我查询 mid+180 就可以知道答案在 [l,mid-1] 还是 [mid,r] 了。
直接做最大查询次数是 11 次,已经很好了但是还需要优化(限 10 次)。
我们注意这样一件事情:
第一次的区间长度只有 360。
对于 [l,r] 的区间,中点为 mid
如果我查询 mid+120,那么:
如果是 perfect,那么答案限制在自己这边,并且左端点可以直接限制到 mid+40。
如果是 Good,那么答案限制在 [mid-40,mid+40],区间长度直接缩到了 80,肯定没问题。
剩下的情况都是另一边了,并且右端点可以限制到 mid-40。
注意到第一轮这样操作之后,最大可能出现的区间是 60+80=140,此时直接二分仍然是不够通过的。
我们还能继续减少次数:
我们查询 mid+170,那么:
如果是 Perfect 或者 Good,那么答案限制在自己这边。
如果是 Bad,那么答案限制在 [mid-10,mid+10],区间长度直接缩到了 20,肯定没问题。
剩下的情况都是另一边了,并且右端点可以直接限制到 mid-10。
注意到第二轮这样操作之后,最大可能出现的区间是 60 的,直接二分已经可过了。
此题可解。
*/
ans = rand() % 1001 - 400;
cnt = 0;
l = -400, r = 600;
a[1] = -220;
#ifndef _MSC_VER
cout << "? " << a[1] << endl;
cin >> s[1];
#else
s[1] = query(a[1]);
#endif
if (s[1] != "Miss") r = a[1] + 180;
else l = a[1] + 181;
a[2] = 420;
#ifndef _MSC_VER
cout << "? " << a[2] << endl;
cin >> s[2];
#else
s[2] = query(a[2]);
#endif
if (s[2] != "Miss") l = max(l, a[2] - 180);
else r = min(a[2] - 181, r);
//一轮特殊缩减
mid = l + r >> 1;
a[3] = mid + 120;
#ifndef _MSC_VER
cout << "? " << a[3] << endl;
cin >> s[3];
#else
s[3] = query(a[3]);
#endif
if (s[3] == "Good") l = mid - 40, r = mid + 40;
else if (s[3] != "Perfect") r = mid - 40;
else l = mid + 40;
//二轮特殊缩减
mid = l + r >> 1;
a[4] = mid + 170;
#ifndef _MSC_VER
cout << "? " << a[4] << endl;
cin >> s[4];
#else
s[4] = query(a[4]);
#endif
if (s[4] == "Bad") {
l = mid - 10; r = mid + 10;
int ans = search();
#ifndef _MSC_VER
cout << "! " << ans << endl;
#else
verify(ans);
#endif
continue;
}
else if (s[4] == "Miss") r = mid - 10;
else l = mid + 10;
//三轮特殊缩减
mid = l + r >> 1;
a[5] = mid + 170;
#ifndef _MSC_VER
cout << "? " << a[5] << endl;
cin >> s[5];
#else
s[5] = query(a[5]);
#endif
if (s[5] == "Bad") {
l = mid - 10; r = mid + 10;
int ans = search();
#ifndef _MSC_VER
cout << "! " << ans << endl;
#else
verify(ans);
#endif
continue;
}
else if (s[5] == "Miss") r = mid - 10;
else l = mid + 10;
int ans = search();
#ifndef _MSC_VER
cout << "! " << ans << endl;
#else
verify(ans);
#endif
continue;
}
return 0;
}