C. Searching Local Minimum ###K ###K //K

题目链接:https://codeforces.ml/contest/1480/problem/C

题意:交互题, 不超过100次的询问下, 找出一个长度为n的排列中的谷底  0和n+1为正无穷

思路:100次以内很容易想到二分 ,  不过这是无单调性的二分

每次check a mid  和a mid+1  如果a mid 更小说明肯定在左半区间

否则肯定在右半区间   因为0和n+1是正无穷, 所以 假设 x是小的  x+1是大的, x的左边0的位置有一个最高点,

所以1~x中一定有一个最低点

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e4+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 
11 
12 
13 int main()
14 {
15     /*ios::sync_with_stdio(0);
16     cin.tie(0);*/
17     int n;
18     cin>>n;
19     int l=1,r=n;
20     while(l<r)
21     {
22         int mid=l+r>>1;
23         int x;
24         cout<<"? "<<mid<<'\n';
25         cin>>x;
26 
27         int y;
28         cout<<"? "<<mid+1<<'\n';
29         cin>>y;
30 
31         if(x<y) r=mid;
32         else l=mid+1;
33     }
34     cout<<"! "<<l<<'\n';
35 
36 
37 
38 }
View Code
posted @ 2021-02-10 12:10  canwinfor  阅读(151)  评论(0)    收藏  举报