题目链接

\(Description\)

  有一个长为\(n\)的二进制串,保证\(01\)都存在。你可以询问不超过\(15\)次,每次询问你给出一个长为\(n\)的二进制串,交互库会返回你的串和目标串的不同位的数目。求任意一个\(0\)和\(1\)的位置。

  \(n\leq 1000\)。

\(Solution\)

  通过\(0...\)和\(1...\)就可以判断出第一个数是什么。然后二分,对每个区间判断是否全为\(0/1\),就可以找到另一个了。

  为啥有时候用read()会T。。

  1. #include <cstdio>
  2. #include <cctype>
  3. #include <cstring>
  4. #include <algorithm>
  5. #define gc() getchar()
  6. const int N=1005;
  7. int n;
  8. char s[N];
  9. inline int read()
  10. {
  11. int now=0;register char c=gc();
  12. for(;!isdigit(c);c=gc());
  13. for(;isdigit(c);now=now*10+c-'0',c=gc());
  14. return now;
  15. }
  16. inline int Query()
  17. {
  18. putchar('?'), puts(s); fflush(stdout);
  19. int t; scanf("%d",&t); return t;
  20. // return read();
  21. }
  22. inline void Fill(int l,int r,const char c)
  23. {
  24. for(int i=l; i<=r; ++i) s[i]=c;
  25. }
  26. int Solve(const char q,const char std)
  27. {
  28. for(int i=1; i<=n; ++i) s[i]=std;
  29. int l=2,r=n,mid,bef=Query(),tmp;
  30. while(l<r)
  31. {
  32. Fill(l,mid=l+r>>1,q), tmp=Query(), Fill(l,mid,std);
  33. if(std::abs(bef-tmp)==mid-l+1 && bef>tmp) return l;//注意等于区间长度时差值可能是不变的
  34. if(std::abs(bef-tmp)<mid-l+1) r=mid;
  35. else l=mid+1;
  36. }
  37. return l;
  38. }
  39. int main()
  40. {
  41. scanf("%d",&n); //n=read();
  42. for(int i=1; i<=n; ++i) s[i]='0'; s[0]=' ', s[n+1]='\0';
  43. int t0=Query(); s[1]='1'; int t1=Query();
  44. int ans0, ans1;
  45. if(t0<t1) ans0=1, ans1=Solve('1','0');
  46. else ans1=1, ans0=Solve('0','1');
  47. printf("! %d %d\n",ans0,ans1);
  48. return 0;
  49. }

Codeforces.862D.Mahmoud and Ehab and the binary string(交互 二分)的更多相关文章

  1. Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互)

    <题目链接> 题目大意: 有一个长度为n(n<1000)的01串,该串中至少有一个0和一个1,现在由你构造出一些01串,进行询问,然后系统会给出你构造的串与原串的   Hamming ...

  2. Codeforces 862D. Mahmoud and Ehab and the binary string (二分)

    题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...

  3. codeforces D. Mahmoud and Ehab and the binary string(二分)

    题目链接:http://codeforces.com/contest/862/submission/30696399 题解:这题一看操作数就知道是二分答案了.然后就是怎么个二分法,有两种思路第一种是找 ...

  4. D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

    http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 #include <cstdio ...

  5. 【二分】Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string

    题意:交互题:存在一个至少有一个0和一个1的长度为n的二进制串,你可以进行最多15次询问,每次给出一个长度为n的二进制串,系统返回你此串和原串的海明距离(两串不同的位数).最后要你找到任意一个0的位置 ...

  6. Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)

    Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...

  7. Codeforces 862A Mahmoud and Ehab and the MEX

    传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...

  8. Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)

    Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...

  9. Codeforces 862B - Mahmoud and Ehab and the bipartiteness

    862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...

随机推荐

  1. bzoj千题计划201:bzoj1820: [JSOI2010]Express Service 快递服务

    http://www.lydsy.com/JudgeOnline/problem.php?id=1820 很容易想到dp[i][a][b][c] 到第i个收件地点,三个司机分别在a,b,c 收件地点的 ...

  2. c#的as,is 运算符

  3. 转----MarkdownPad2.5 注册码

    经测试可用 User: Soar360@live.com 授权: GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDHzWdD6xhMNLGVpbP2 ...

  4. CSS-3 box-shadow 的使用

    box-shadow是给对象实现图层阴影效果的. 语法: E {box-shadow: <length> <length> <length>?<length& ...

  5. 经典设计模式-iOS的实现

    最近看了<HeadFirst 设计模式>这本书,给组内伙伴准备一次分享,把这次分享记录下来,有需要的可以看看. 这本书主要介绍了四人帮23种经典设计模式中的的14种,也是常用的几种.看完这 ...

  6. UVALive 7456 Least Crucial Node

    题目链接 题意: 给定一个无向图,一个汇集点,问哪一个点是最关键的,如果有多个关键点,输出序号最小的那个. 因为数据量比较小,所以暴力搜索就行,每去掉一个点,寻找和汇集点相连的还剩几个点,以此确定哪个 ...

  7. MongoDB 之 "$" 的奇妙用法 MongoDB - 5

    在MongoDB中有一个非常神奇的符号 "$" "$"  在 update 中 加上关键字 就 变成了 修改器 其实 "$" 字符 独立出现 ...

  8. MySQL多源复制【转】

    什么是多源复制? 首先,我们需要清楚 multi-master 与multi-source 复制不是一样的. Multi-Master 复制通常是环形复制, 你可以在任意主机上将数据复制给其他主机. ...

  9. 在VMware上安装Ubuntu软件步骤与遇到的相关问题及解决方案

    图解演示环境版本: 本机系统: WIN10 虚拟机:VMware Workstation 12(中文版) 安装目标:Ubuntu Desktop 12.04 LTS  (请点击这里)先下载好iso镜像 ...

  10. 通过anaconda进行python多版本控制

    ---恢复内容开始--- linux与windows通用. 1. 假设电脑上已经转好anaconda3. (anaconda 默认装好了python3.jupyter.spyter) 2. 现在需求是 ...