【二分】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个数,每个 ...
随机推荐
- Vue组件-动态组件
动态组件 通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以让多个组件使用同一个挂载点,并动态切换: <div id="app6"& ...
- Linux进程调度与源码分析(三)——do_fork()的实现原理
用户层的fork(),vfork(),clone()API函数在执行时,会触发系统调用完成从用户态陷入到内核态的过程,而上述函数的系统调用,最终实现都是通过内核函数do_fork()完成,本篇着重分析 ...
- python基础===Number
本文转自:python之Number 1.Python number数字 Python Number 数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将 ...
- linux内核启动分析(3)
主要分析do_basic_setup函数里面的do_initcalls()函数,这个函数用来调用所有编译内核的驱动模块中的初始化函数. static void __init do_initcalls( ...
- centos 引导盘
# grub.conf generated by anaconda## Note that you do not have to rerun grub after making changes to ...
- 64_k1
KoboDeluxe-0.5.1-22.fc26.x86_64.rpm 13-Feb-2017 22:11 1626454 k3b-17.04.1-1.fc26.x86_64.rpm 25-May-2 ...
- MSCL超级工具类(C#),开发人员必备,开发利器
MSCL超强工具类库 是基于C#开发的超强工具类集合,涵盖了日常B/S或C/S开发的诸多方面,包含上百个常用封装类(数据库操作类全面支持Mysql.Access.Oracle.Sqlserver.Sq ...
- CF914F Substrings in a String
Description 给你一个字符串ss,共有qq次操作,每个都是下面两种形式的一种. 11 ii cc 这个操作表示将字符串ss的第ii项变为字符cc 22 ll rr yy 这个操作表示输出字符 ...
- java中的i++与++i有什么区别?
刚开始接触时,做了一些小测试,还以为这两个没有什么区别. public class OperatorDemo { public static void main(String[] args){ int ...
- 【python】多个文件共用日志系统的重复打印问题
先写一个最简单的log文件: test_logging5.py #coding:utf-8 import logging logging.debug('logger debug message') l ...