【二分】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个数,每个 ...
随机推荐
- 打印菱形(c语言)
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { // 定 ...
- O(1)时间复杂度实现入栈、出栈、获得栈中最小元素、获得栈中最大元素(转)
题目要求:定义栈的数据结构,添加min().max()函数(动态获取当前状态栈中的最小元素.最大元素),要求push().pop().min().max()的时间复杂度都是O(1). 思路解析:根据栈 ...
- hydra 密码破解工具详解
一.简介 hydra是著名黑客组织thc的一款开源的暴力密码破解工具,可以在线破解多种密码.官 网:http://www.thc.org/thc-hydra,可支持AFP, Cisco AAA, Ci ...
- 使用JMX工具远程监控tomcat配置
使用JMX工具远程监控tomcat,在tomcat启动时添加配置参数: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.po ...
- Shell三剑客之sed命令
Sed简介 Sed是Stream Editor(流编辑器)缩写,是操作.过滤和转换文本内容的强大工具,常用功能有增删改查. Sed命令执行流程 Sed语法格式 Sed [option] ‘[匹配][处 ...
- (转)函数后面加const--C++ const成员函数
类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非静态数据成员)作任何改变. 在设计类的时候,一个原则就是对于不改变数据成员的成员函数都要在后面加 const,而对于 ...
- linux磁盘占用跟每个文件夹大小总和不符
1.一种情况是删除了大文件但是没有释放出来,因为有进程还在调用使用 最简单的方法是reboot下服务器再对比下: 2.查看服务器空间使用情况 df -h cd / du -sh *
- Longest Substring Without Repeating Characters——经典题
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [转载]Python命令行参数学习
转载自: http://blog.163.com/weak_time/blog/static/25852809120169333247925/ Python的命令行参数,提供了很多有用的功能,可以方便 ...
- ssm框架问题和Java
http://blog.csdn.net/zzjjiandan/article/details/20853233 BeanFactory的作用是什么? BeanFactory是配置.创建.管理bean ...