交互题
https://vjudge.net/contest/283149#problem/I 专题
Subway Pursuit
题意:猜火车的位置,
每次区间缩小到一定程度就可以用rand函数猜一个数字,猜对了就结束,猜错了之后,在这个区间火车可能会向左走K,向右走K。4k是一个人为订的范围,指区间变得这么大的时候就才一个数。大一点也行。
#include<cstdio> #include<algorithm> #include<cstring> #include<cstdlib> #include<ctime> using namespace std; typedef long long ll; ll n, k; char s[10]; int main() { srand(time(0)); scanf("%lld%lld", &n, &k); ll l = 1, r = n; while(1){ if(r - l <= (ll)4 * k){ ll x = rand() % (r - l + 1) + l; printf("%lld %lld\n", x, x); fflush(stdout); scanf("%s", s); if(s[0] == 'Y' || s[0] == 'B') return 0; else{ l = max(1ll, l - k); r = min(n, r + k); } } else{ ll mid = (l + r) / 2; printf("%lld %lld\n", l, mid); fflush(stdout); scanf("%s", s); if(s[0] == 'B') return 0; else if(s[0] == 'Y'){ l = max(1ll, l - k); r = min(n, mid + k); } else{ l = max(1ll, mid + 1 - k); r = min(n, r + k); } } } return 0; }
The hat
尽量还是用cout
相邻的两个人相差1或-1(圆的), 求那个人与对面那个人的树枝相等。必须是4K个人才有可能相等。
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; const int maxm = 1e5 + 5; typedef long long ll; int n; int fun(int x) { int l ,r; cout << "? " << x <<endl; cin >> l; cout << "? " << x + n / 2 << endl; cin >> r; if(r - l == 0) { cout << "! " << x <<endl; exit(0); } return r - l; } int main() { cin >> n; if(n % 4) { cout << "! -1" << endl; return 0; } int l = 1, r = n / 2, mid; while(l <= r) { mid = (l + r) / 2; ll vl = fun(l); ll ml = fun(mid); if(vl * ml < 0) { r = mid; } else { l = mid + 1; } } cout << "! -1" << endl; return 0; }