二分交互题总结

简单版

cpp 复制代码
bool query(int x){
    cout<<x<<endl;
    cout.flush();
    string ans;cin>>ans;
    return (ans==">=");
}
void solve()
{
    int n;cin>>n;
    int l=1,r=n;
    if(n==1)return cout<<"! 1"<<endl,void();
    if(n==2){
        if(query(2))return cout<<"! 2"<<endl,void();
        else return cout<<"! 1"<<endl,void();
    }
    int cnt=0;
    while (l<r)
    {
        int mid=(l+r+1)>>2;
        // cout<<l<<' '<<r<<endl;
        if(query(mid)){
            l=mid;
        }else r=mid-1;
        // cout<<l<<' '<<r<<endl;
    }
    cout<<"! "<<l<<endl;
}

进阶

限制四次之内找到 x ′ ∈ [ x 2 , 2 x ] x'\in[{x\over2},2x] x′∈[2x,2x]

思路:

  • x越大 范围越大
  • 打表 找5e9内 [ x 2 , 2 x ] [{x\over2},2x] [2x,2x]的所有区间 每个区间设置一个固定输出值
  • 打完表惊奇地发现区间个数只有16个,四次份 2 4 2^4 24个段,能确定 x ′ x' x′的范围
  • 每次回答问的数y是否>x,0为 y ≤ x y\leq x y≤x
  • 设计问下边界,因为越小区间越短越密集越精确
  • 最后输出区间中间的数
cpp 复制代码
int lo[17],hi[17];
void solve()
{
    int n;cin>>n;
    if(n==1)return cout<<"! 1"<<endl,void();
    int k=2;
    lo[1]=1,hi[1]=4;
    // cout<<lo[1]<<' '<<hi[1]<<endl;
    while (hi[k-1]<n)
    {
        lo[k]=hi[k-1]+1;
        hi[k]=min(lo[k]*4,n);//注意上边界卡在n内 否则会输出超出[1,n]的数
        // cout<<lo[k]<<' '<<hi[k]<<endl;
        k++;
    }
    int l=1,r=k-1;
    while (l<r)
    {
        int mid=(l+r+1)>>1;
        cout<<"? "<<lo[mid]<<endl;
        cout.flush();
        int re;
        cin>>re;
        if(re==1){
            r=mid-1;
        }else{
            l=mid;
        }
    }
    cout<<"! "<<min(lo[l]*2,hi[l])<<endl;
    //一开始输出答案忘了换行 竟然T了 
}
相关推荐
myw07120514 分钟前
Leetcode94.二叉数的中序遍历练习
c语言·数据结构·笔记·算法
songx_9920 分钟前
leetcode(填充每个节点的下一个右侧节点指针 II)
java·数据结构·算法·leetcode
chenyuhao202421 分钟前
vector深度求索(上)实用篇
开发语言·数据结构·c++·后端·算法·类和对象
小小测试开发1 小时前
Python + MediaPipe 手势绘画高级应用:从基础到创意交互
开发语言·python·交互
minstbe1 小时前
半导体数据分析:GPR算法小白入门(三) 晶体管I-V特性仿真教程
算法
未知陨落2 小时前
LeetCode:60.单词搜索
算法·leetcode
mmz12072 小时前
动态规划 练习(c++)
c++·算法·动态规划
tqs_123453 小时前
分sheet写入excel
开发语言·python·算法
西望云天3 小时前
基础组合计数(三道例题)
数据结构·算法·icpc
小灰灰的FPGA4 小时前
29.9元汉堡项目:基于matlab+FPGA的FFT寻峰算法实现
算法·matlab·fpga开发