Guessing the Greatest

题目链接:https://codeforces.ml/problemset/problem/1486/C2

题意:交互题 每次查询一段区间 返回你次大值, n个不同的数中 在20次内找出 最大值

思路:第一次找1~n  找出  次大值 smx  然后  通过查询1~sm看最大值在左边还是右边,假设在右边

的话 那么就是找 最小的m满足 smx~m 返回的是smx

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e5+10;
 4 const int mod=1e9+7;
 5 #define ll long long
 6 #define pi pair<int,int>
 7 #define fi first
 8 #define sc second
 9 #define pb push_back
10 int l,r,smx;
11 int check(int x)
12 {
13     int p;
14     cout<<"? "<<x<<" "<<smx<<'\n';
15     cin>>p;
16     if(p==smx) return 1;
17     return 0;
18 }
19  
20 int check2(int x)
21 {
22     int p;
23     cout<<"? "<<smx<<" "<<x<<'\n';
24     cin>>p;
25     if(p==smx) return 1;
26     return 0;
27 }
28  
29 int main()
30 {
31     /*ios::sync_with_stdio(0);
32     cin.tie(0);*/
33     int n;
34     cin>>n;
35     cout<<"? "<<1<<" "<<n<<'\n';
36     cin>>smx;
37     int c;
38     if(smx==1)
39     {
40         l=smx+1,r=n;
41         while(l<r)
42         {
43             int mid=(l+r)/2;
44             if(check2(mid))
45             {
46                 r=mid;
47             }
48             else
49                 l=mid+1;
50         }
51         cout<<"! "<<l<<'\n';
52     }
53     else
54     {
55         cout<<"? "<<1<<" "<<smx<<'\n';
56         cin>>c;
57         if(c==smx)
58         {
59             l=1,r=smx-1;
60             while(l<r)
61             {
62                 int mid=(l+r+1)/2;
63                 if(check(mid))
64                 {
65                     l=mid;
66                 }
67                 else
68                     r=mid-1;
69             }
70             cout<<"! "<<l<<'\n';
71         }
72         else
73         {
74             l=smx+1,r=n;
75             while(l<r)
76             {
77                 int mid=(l+r)/2;
78                 if(check2(mid))
79                 {
80                     r=mid;
81                 }
82                 else
83                     l=mid+1;
84             }
85             cout<<"! "<<l<<'\n';
86         }
87     }
88  
89  
90  
91  
92  
93  
94  
95  
96 }
View Code

 

posted @ 2021-02-19 23:11  canwinfor  阅读(101)  评论(0)    收藏  举报