其他分享
首页 > 其他分享> > Codeforces Round #703 (Div. 2) C2. Guessing the Greatest(二分,交互)

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

作者:互联网

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;
}

 

标签:Guessing,typedef,int,ll,Codeforces,long,703,include,define
来源: https://www.cnblogs.com/studyshare777/p/14423530.html