码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #703 (Div. 2) C2. Guessing the Greatest(二分,交互)

时间:2021-02-22 12:10:32      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:clu   color   pos   while   define   long   The   targe   ons   

https://codeforces.com/contest/1486/problem/C2

#include<cstdio>
#include<iostream>
#include<deque>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#include<queue>
#include<set>
//#include<bits/stdc++.h>
#define sd(x) scanf("%d",&x)
#define lsd(x) scanf("%lld",&x)
#define sf(x) scanf("%lf",&x)
#define ms(x,y) memset(x,y,sizeof x)
#define fu(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define range(a,x,y) a+x,a+y+x
using namespace std;
using namespace __gnu_cxx;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> P;
const int N=1e5+99;
ll mod=2147493647;
const ll INF=1e15+7;
struct od
{
    int x,y,nod;
}e[N];
ll n,m,dis[N],cost[N],pos[N];
vector<P> to[N];
set<ll> node;
ll a[N],b[N];
int q(int l,int r)
{
    printf("? %d %d\n",l,r);fflush(stdout);
    int mid;sd(mid);
    return mid;
}
int main() 
{
    int n;sd(n);
    int l=1,r=n,mid,ans;
    int p=q(l,r);
    if(p>1&&p==q(1,p))
    {
        l=1,r=p-1;//左闭右闭
        //最大在p左边,二分找最大满足p==(pos,p)的pos
        while(l<=r)
        {
            mid=(l+r)/2;
            if(p==q(mid,p)) l=mid+1,ans=mid;
            else r=mid-1;
        }
     }
    else
    {
        l=p+1,r=n;//左闭右闭
        //最大在p右边,二分找最小满足p==(p,pos)的pos
        while(l<=r)
        {
            mid=(l+r)/2;
            if(p==q(p,mid)) r=mid-1,ans=mid;
            else l=mid+1;
        }
    }
    printf("! %d\n",ans);fflush(stdout);
    return 0;
}

 

Codeforces Round #703 (Div. 2) C2. Guessing the Greatest(二分,交互)

标签:clu   color   pos   while   define   long   The   targe   ons   

原文地址:https://www.cnblogs.com/studyshare777/p/14423530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!