題意
互動題:給定一個 01 序列 (),保證 且 。你可以詢問至多 20 次某個位置的值,求任意一個 使得 。
解析
考慮二分,如果 ,則說明 到 中間存在可行的 ,如果 則說明 到 存在可行 。
int main()
{
int n;
std::cin >> n;
int l = 1, r = n;
std::map<int, int> cache;
auto ask = [&cache](int pos) mutable -> int
{
if (cache.count(pos) != 0) {
return cache[pos];
} else {
std::cout << "? " << pos << std::endl << std::flush;
int res;
std::cin >> res;
cache[pos] = res;
return res;
}
};
while (l < r) {
int mid = (l + r) / 2;
int s1 = ask(mid);
if (s1 == 1) {
r = mid;
} else {
l = mid + 1;
}
}
std::cout << "! " << r - 1 << std::endl << std::flush;
}