【二分】Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string
题意:交互题:存在一个至少有一个0和一个1的长度为n的二进制串,你可以进行最多15次询问,每次给出一个长度为n的二进制串,系统返回你此串和原串的海明距离(两串不同的位数)。最后要你找到任意一个0的位置和任意一个1的位置。
先问n个0,返回你的原串中是1的数量,记为X。然后将任意一位改成1,倘若答案增量为1,那么你改动的那位在原串中是0,反之,那位是1。
不失一般性地,我们假设你改动的那一位在原串中是0,你就要去找1的位置。
就不断二分缩小区间,每次将当前区间划分成左右两部分l,m m+1,r。如果左半区间存在1,就去查左区间,否则去查右区间。
一个区间[L,R]没有1的充要条件是,询问0...01...10...0,中间的1恰好对应[L,R]区间,则返回的答案Y减去X恰好等于R-L+1;反之有1。
#include<cstdio>
using namespace std;
int n,x,y,pos[2];
int main(){
scanf("%d",&n);
printf("? ");
for(int i=1;i<=n;++i){
putchar('0');
}
puts("");
fflush(stdout);
scanf("%d",&x);
printf("? ");
putchar('1');
for(int i=2;i<=n;++i){
putchar('0');
}
puts("");
fflush(stdout);
scanf("%d",&y);
if(y==x+1){
pos[0]=1;
int l=1,r=n;
while(r-l>0){
int m=(l+r>>1);
printf("? ");
for(int i=1;i<l;++i){
putchar('0');
}
for(int i=l;i<=m;++i){
putchar('1');
}
for(int i=m+1;i<=n;++i){
putchar('0');
}
puts("");
fflush(stdout);
scanf("%d",&y);
if(y-x!=m-l+1){
r=m;
}
else{
l=m+1;
}
}
pos[1]=l;
printf("! %d %d\n",pos[0],pos[1]);
}
else{
pos[1]=1;
int l=1,r=n;
while(r-l>0){
int m=(l+r>>1);
printf("? ");
for(int i=1;i<l;++i){
putchar('0');
}
for(int i=l;i<=m;++i){
putchar('1');
}
for(int i=m+1;i<=n;++i){
putchar('0');
}
puts("");
fflush(stdout);
scanf("%d",&y);
if(x-y!=m-l+1){
r=m;
}
else{
l=m+1;
}
}
pos[0]=l;
printf("! %d %d\n",pos[0],pos[1]);
}
return 0;
}
【二分】Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string的更多相关文章
- 【构造】【分类讨论】Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
题意:给你n,x,均不超过10^5,让你构造一个无重复元素的n个元素的非负整数集合(每个元素不超过10^6),使得它们的Xor和恰好为x. 如果x不为0: 随便在x里面找一个非零位,然后固定该位为0, ...
- Codeforces Round #435 (Div. 2)【A、B、C、D】
//在我对着D题发呆的时候,柴神秒掉了D题并说:这个D感觉比C题简单呀!,,我:[哭.jpg](逃 Codeforces Round #435 (Div. 2) codeforces 862 A. M ...
- 【Codeforces Round #435 (Div. 2) A B C D】
CF比赛题目地址:http://codeforces.com/contest/862 A. Mahmoud and Ehab and the MEX ·英文题,述大意: 输入n,x(n,x& ...
- Codeforces 862D. Mahmoud and Ehab and the binary string (二分)
题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集
D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary
地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑
E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...
- Codeforces Round #396 (Div. 2) A. Mahmoud and Longest Uncommon Subsequence 水题
A. Mahmoud and Longest Uncommon Subsequence 题目连接: http://codeforces.com/contest/766/problem/A Descri ...
- Codeforces Round #435 (Div. 2)
A. Mahmoud and Ehab and the MEX 题目链接:http://codeforces.com/contest/862/problem/A 题目意思:现在一个数列中有n个数,每个 ...
随机推荐
- React组件生命周期小结
React组件生命周期小结 下面所写的,只适合前端的React.(React也支持后端渲染,而且和前端有点小区别,不过我没用过.) 相关函数 简单地说,React Component通过其定义的几个函 ...
- JVM在遇到OOM(OutOfMemoryError)时生成Dump文件
方法一: 命令:jmap -dump:format=b,file=heap.bin file:保存路径及文件名pid:进程编号(windows通过任务管理器查看,linux通过ps aux查看) du ...
- a标签的嵌套
1.a标签的嵌套 a标签不能嵌套,若a标签中嵌套了a标签,浏览器会自动添加结束符号,故不能嵌套 2.例子 编辑器中的代码 <a href="#">外层a标签<a ...
- Java多线程学习(五)线程间通信知识点补充
系列文章传送门: Java多线程学习(二)synchronized关键字(1) Java多线程学习(二)synchronized关键字(2) Java多线程学习(三)volatile关键字 Java多 ...
- 破解邻居家的wifi密码
刚刚学习了如何破解wifi密码 然后昨天晚上连续破解了两个 好激动 我是在ubuntu上面使用aircrack-ng套件进行破解的 首先进行抓包,然后跑字典就ok了 下面的不错 一.关闭网络和结束可能 ...
- Intel call指令
转载:http://blog.ftofficer.com/2010/04/n-forms-of-call-instructions/ 最近有一个需求,给你个地址,看看这个地址前面是不是一个CALL指令 ...
- Python Matplotlib图表汉字显示成框框的解决办法
http://blog.sina.com.cn/s/blog_662dcb820102vu3d.html http://blog.csdn.net/fyuanfena/article/details/ ...
- PHP-5.6.22安装
查看系统及内核版本 [root@test88 ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@test88 ~]# uname ...
- 使用cmd(黑窗口)敲命令使用远程数据库
C:\Users\gzz>mysql -h 10.27.104.176 -u root -p mysql
- Activiti如何替换已部署流程图
首先交代下背景:我们有一个已经上线的activiti工作流系统,对于流程图的操作已经封装好部署,查看,删除的接口.此时客户提出要修改个别流程图里的节点名称. 我的第一个想法就是本地修改流程图bpmn文 ...