消失的兔子~~~

   做法

  1. 想法:看数值都知道是二分

  2. 把答案分成两个部分:左和右(把c=5e8-1)。 问1的时候若回答是1: 代表他在[1,5e8]里 要不然就是[5e8+1,1e9]

  3. 若在左:二分右边。右边相似

  4. 找最后(若兔子是在右边),最早(若在左边)的点的值是1. 再加或减掉c就可以啦~~~~

 

具体代码:

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define int long long
 4 signed main() {
 5     ios_base::sync_with_stdio(0); cin.tie(0);
 6     int tt; cin >> tt;
 7     while (tt--) {
 8         int ok = 0, c = 5e8 - 1;
 9         auto ask = [&](int pos) {
10             cout << pos << endl;
11             int response; cin >> response;
12             if (response == 2) ok = 1;
13             return response;
14         };
15         cout << c << endl;
16         int left = ask(1);
17         if (ok) continue;
18         if (left) {
19             int l = 5e8, r = 1e9;
20             while (l < r) {
21                 int mid = (l + r + 1) / 2;
22                 if (ask(mid)) {
23                     l = mid;
24                 }
25                 else {
26                     r = mid - 1;
27                 }
28                 if (ok) break;
29             }
30             if (ok) continue;
31             ask(l - c);
32         }
33         else {
34             int l = 1, r = 5e8 + 1;
35             while (l < r) {
36                 int mid = (l + r - 1) / 2;
37                 if (ask(mid)) {
38                     r = mid;
39                 }
40                 else {
41                     l = mid + 1;
42                 }
43                 if (ok) break;
44             }
45             if (ok) continue;
46             ask(r + c);
47         }
48     }
49 }

 

posted on 2023-07-10 17:10  yl_neo  阅读(75)  评论(0编辑  收藏  举报