我直接复制粘贴题面中的 std 和交互库:
#include <cstdio> // 在本题中并不是必须的
extern "C" int Seniorious(int); // 在这里需要声明一次交互库给出的函数。
extern "C" int Chtholly(int n, int OvO) { // 在这里实现交互库要求你实现的函数。
int ans = 1;
for (int l = 1, r = n, mid = (l + r) >> 1; l <= r; mid = (l + r) >> 1) if (Seniorious(mid) >= 0) {
r = (ans = mid) - 1;
} else {
l = mid + 1;
}
return ans;
}
#include <cstdio>
#include <iostream>
int k, cnt;
extern "C" {
extern int Chtholly(int n, int c);
extern int Seniorious(int x) {
const int lim = 3000000;
if(++cnt > lim) cnt = lim;
return (k < x) ? 1 : ((k == x) ? 0 : -1);
}
}
int main() {
int n, c;
std::cin >> n >> c >> k;
int OvO = Chtholly(n, c);
std::cout << OvO << ' ' << cnt << std::endl;
return 0;
}
输入:
2 1 2
在 [1,2] 中猜数字 2,只允许交互 1 次。这个交互次数符合 min(20,n−1)≤c≤n 的规定。然而输出为
2 2
也就是 std 调用了两次交互库。是否需要加入边界情况的特判?