CF843B Interactive LowerBound 题解
gaochunzhen · · 个人记录
省流:随机撒点。
对于
对于其他情况,可以询问
如果
如果这个最大值恰为
否则就从最大值的下一项开始询问,直到找到答案或者下一项为
后注:随机选点的时候随机数要比较随性,不要直接用 rand()%n+1 或者 mt19937,用类似于 ((unsigned int)rand()*rand()+rand())%n+1 之类的东西。
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(random_device{}());
int n, st, p, a[50009][2];
bool vis[50009];
int main() {
scanf("%d%d%d", &n, &st, &p);
if (n < 2000) {
int ans = 2e9, x, nxt;
for (int i = 1; i <= n; i++) {
printf("? %d\n", i), fflush(stdout);
scanf("%d%d", &x, &nxt);
if (x >= p) ans = min(x, ans);
}
printf("! %d\n", ans == 2e9 ? -1 : ans), fflush(stdout);
return 0;
}
vis[st] = 1;
int maxx, id;
printf("? %d\n", st), fflush(stdout);
scanf("%d%d", &maxx, &id);
if (maxx >= p) {
printf("! %d\n", maxx), fflush(stdout);
return 0;
}
for (int i = 1; i <= 1000; i++) {
int x = 1ull * rng() * rng() % n + 1;
while (vis[x]) x = 1ull * rng() * rng() % n + 1;
printf("? %d\n", x), fflush(stdout);
scanf("%d%d", &a[x][0], &a[x][1]);
if (a[x][0] <= p && a[x][0] > maxx) maxx = a[x][0], id = a[x][1];
vis[x] = 1;
}
if (maxx == p) {
printf("! %d\n", maxx), fflush(stdout);
return 0;
}
while (maxx < p && id > 0) {
if (vis[id]) {
maxx = a[id][0];
break;
}
printf("? %d\n", id), fflush(stdout);
scanf("%d%d", &maxx, &id);
}
printf("! %d\n", maxx >= p ? maxx : -1), fflush(stdout);
return 0;
}