题意:交互题:存在一个至少有一个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的更多相关文章

  1. 【构造】【分类讨论】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, ...

  2. Codeforces Round #435 (Div. 2)【A、B、C、D】

    //在我对着D题发呆的时候,柴神秒掉了D题并说:这个D感觉比C题简单呀!,,我:[哭.jpg](逃 Codeforces Round #435 (Div. 2) codeforces 862 A. M ...

  3. 【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& ...

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

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

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #435 (Div. 2)

    A. Mahmoud and Ehab and the MEX 题目链接:http://codeforces.com/contest/862/problem/A 题目意思:现在一个数列中有n个数,每个 ...

随机推荐

  1. php伪协议

    文件包含函数:include.require.include_once.require_once.highlight_file .show_source .readfile .file_get_con ...

  2. Python模块学习 - openpyxl

    openpyxl模块介绍 openpyxl模块是一个读写Excel 2010文档的Python库,如果要处理更早格式的Excel文档,需要用到额外的库,openpyxl是一个比较综合的工具,能够同时读 ...

  3. WmiPrvSE.exe进程(WMI Provider Host)不能删除

    WmiPrvSE.exe进程基本信息:程序厂商:微软® Microsoft Corp.进程描述:WMI Provider Host进程属性:Windows系统进程使用网络:是的启动情况:触发启动 来历 ...

  4. appium===登陆应用的案例

    import time import os from appium import webdriver from selenium.webdriver.support.ui import WebDriv ...

  5. python基础===codecs打开文件,解决文件编码格式的问题

    codecs https://docs.python.org/3/library/codecs.html 我们经常用open打开文件的时候会出现各式各样的错误,编码格式的问题,等等~真的很烦 现在尽量 ...

  6. monkey测试===monkeyrunner测试教程(2)

    我先引入一段代码: #test.py from com.android.monkeyrunner import MonkeyRunner as mr device=mr.waitForConnecti ...

  7. interrupted()和isInterrupted()比较+终止线程的正确方法+暂停线程

    interrupted():测试当前线程[运行此方法的当前线程]是否已经是中断状态,执行后具有将状态标志清除为false的功能. isInterrupted():测试线程对象是否已经是中断状态,但不清 ...

  8. Keepalived高可用配置

    Keepalived简介 Keepalived基于VRRP协议在服务器之间建立了主备关系,通常称之为高可用对.VRRP中文叫虚拟路由冗余协议,目的是解决静态路由的单点故障问题.高可用对之间通过IP多播 ...

  9. 类似于input输入框placeholder的效果,兼容ie8

    $(function(){    //判断浏览器是否支持placeholder属性  supportPlaceholder='placeholder'in document.createElement ...

  10. ZOJ-3430

    Detect the Virus  Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his c ...