• 交互题入门


    2022CCPC预选赛K

    题面

    题解

    • 5000次都输的概率小,至少一场能赢
    • 而本金很多,只需要每次都投入上一次投入金钱的两倍即可

    代码

    //没写高精度
    #include 
    #include 
    
    using namespace std;
    
    int main() {
        long long y,q,seed,cnt=1,x;
        cin>>y>>q>>seed;
        
        do {
            cout<<cnt*y<<'\n';
            fflush(stdout);
            cnt*=2;
            cin>>x;
        }while(x!=2);
        
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    B. Lost Numbers

    题意

    • 有一个[4,8,15,16,23,42]的排列,通过最多4次询问得出其排列
    • 每次询问:可以询问两个位置i,j,返回ai*aj

    题解

    • 由于这些数两两乘积不同,所以只需按照(1,2),(2,3),(3,4),(4,5)去询问必然可以得到这6个数的排列
    • 代码写法上,可以枚举所有排列,然后根据交互出来的回答验证某个排列的正确性

    代码

    #include 
    #include 
    
    using namespace std;
    int a[6]={4,8,15,16,23,42};
    int b[6];
    
    bool check() {
        for(int i=0;i<4;i++) 
            if(a[i]*a[i+1]!=b[i])
                return false;
        return true;
    }
    
    int main() {
        for(int i=0;i<4;i++) {//询问
            cout<<"? "<<i+1<<' '<<i+2<<'\n';
            fflush(stdout);
            cin>>b[i];
        }
        do {//马驹排列验证
            if(check()) {
                cout<<"! ";
                for(int i=0;i<6;i++)
                    cout<<a[i]<<' ';
                puts("");
            }
        }while(next_permutation(a,a+6));
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    C. Chocolate Bunny

    题意

    • 有一个n的排列,能否通过最多2n次询问得到这个排列
    • 每次询问:询问i,j,得到ai mod aj的值

    题解

    • 简化,n=2时,a=ask(1,2); b=ask(2,1);如果a>b说明a[1]
    • 推广,用双指针一次确定两个指针中的一个数,直到最后没有确定位置的指针就是数值n所在的位置

    代码

    #include 
    
    using namespace std;
    const int N=1e4+10;
    int ans[N];
    
    int ask(int x,int y) {
        cout<<"? "<<x+1<<' '<<y+1<<'\n';
        fflush(stdout);
        int k;
        cin>>k;
        return k;
    }
    
    int main() {
        int n;
        cin>>n;
        
        int p=0;//左指针
        for(int i=1;i<n;i++) {//右指针
            int a=ask(p,i);
            int b=ask(i,p);
            if(a>b) {
                ans[p]=a;//左指针位置确定
                p=i;//移动左指针到未确定的位置
            }
            else ans[i]=b;//右指针位置确定,for循环中自行移动
        }
        ans[p]=n;
        
        cout<<"! ";
        for(int i=0;i<n;i++) cout<<ans[i]<<' ';
        puts("");
        
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
  • 相关阅读:
    JVM内存配置参数
    Vue开发实例(五)修改项目入口&页面布局
    CentOS 安装HTTP代理服务器 Squid
    ansible Lineinfile模块
    分享Redshift渲染器的去噪方法技巧,一定要看看
    win11微软账户登录一直转圈怎么解决?win11微软账户登录一直转圈
    JVM:字节码文件,类的生命周期,类加载器
    DEJA_VU3D - Cesium功能集 之 057-百度地图纠偏
    MySQL数据库管理
    Java设计模式-结构型模式-适配器模式
  • 原文地址:https://blog.csdn.net/m0_49843646/article/details/126819621