【Codeforces 862 D】Mahmoud and Ehab and the binary string,二分答案,构造,交互题

problem

D. Mahmoud and Ehab and the binary string
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mahmoud and Ehab are in the fourth stage now.

Dr. Evil has a hidden binary string of length n. He guarantees that there is at least one ‘0’ symbol and at least one ‘1’ symbol in it. Now he wants Mahmoud and Ehab to find a position of any ‘0’ symbol and any ‘1’ symbol. In order to do this, Mahmoud and Ehab can ask Dr. Evil up to 15 questions. They tell Dr. Evil some binary string of length n, and Dr. Evil tells the Hamming distance between these two strings. Hamming distance between 2 binary strings of the same length is the number of positions in which they have different symbols. You can find the definition of Hamming distance in the notes section below.

Help Mahmoud and Ehab find these two positions.

You will get Wrong Answer verdict if

Your queries doesn’t satisfy interaction protocol described below.
You ask strictly more than 15 questions and your program terminated after exceeding queries limit. Please note, that you can do up to 15 ask queries and one answer query.
Your final answer is not correct.
You will get Idleness Limit Exceeded if you don’t print anything or if you forget to flush the output, including for the final answer (more info about flushing output below).
If you exceed the maximum number of queries, You should terminate with 0, In this case you’ll get Wrong Answer, If you don’t terminate you may receive any verdict because you’ll be reading from a closed stream .

Input
The first line of input will contain a single integer n (2 ≤ n ≤ 1000) — the length of the hidden binary string.

Output
To print the final answer, print “! pos0 pos1” (without quotes), where pos0 and pos1 are positions of some ‘0’ and some ‘1’ in the string (the string is 1-indexed). Don’t forget to flush the output after printing the answer!

Interaction
To ask a question use the format “? s” (without quotes), where s is a query string. Don’t forget to flush the output after printing a query!

After each query you can read a single integer from standard input — the Hamming distance between the hidden string and the query string.

To flush the output you can use:-

fflush(stdout) in C++;
System.out.flush() in Java;
stdout.flush() in Python;
flush(output) in Pascal;
See the documentation for other languages .
Hacking.

To hack someone just print one binary string with length up to 1000, containing at least one ‘0’ and at least one ‘1’.

Example
inputCopy
3
2
1
3
2
1
0
outputCopy
? 000
? 001
? 010
? 011
? 100
? 101
! 2 1
Note
Hamming distance definition: https://en.wikipedia.org/wiki/Hamming_distance

In the first test case the hidden binary string is 101, The first query is 000, so the Hamming distance is 2. In the second query the hidden string is still 101 and query is 001, so the Hamming distance is 1.

After some queries you find that symbol at position 2 is ‘0’ and symbol at position 1 is ‘1’, so you print “! 2 1”.

solution

/*
题意:
+ 存在一个长为n的01串,题目给出n。可以询问15次,确定01串中某个0和1的位置(题目保证存在)
+ 每次用一个长为n的01串提问,返回具有不同符号的位置数。
思路:
+ 第一次询问由全由0组成的串,得到的就是串中1的个数num1
+ 如果一个区间里只有0,那么构造一个只有区间[L,R]里面全是1,其他地方全是0的序列去询问的话,返回值req = num1+(R-L+1)。如果一个区间里只有0,那么构造一个只有区间[L,R]里面全是0,其他地方全是1的序列去询问的话,返回值req = num1-(R-L+1)。否则返回值就是介于之间的。
+ 此时可以二分答案,去找一个长度为2的"01"或"10"串。当[mid,R]中既有0又有1时L = mid,直到[L,R]区间长度为2为止。最后再询问一次就可以获得答案了。
*/
#include<bits/stdc++.h>
using namespace std;
int n, num1;
bool check(int L, int R){
	string t = string(L-1,'0')+string(R-L+1,'1')+string(n-R,'0');
	cout<<"? "<<t<<"\n";
	fflush(stdout);
	int req;  cin>>req;
	if(req>num1-(R-L+1) && req<num1+(R-L+1))
		return true;
	else 
		return false;
}
int main(){
	cin>>n;
	cout<<"? "<<string(n,'0')<<"\n";
	fflush(stdout);
	cin>>num1;
	
	int L = 1, R = n;
	while(R-L >= 2){
		int mid = (L+R)/2;
		if(check(mid,R))L = mid;
		else R = mid;
	}
	
	string t(n,'0'); t[L-1] = '1';
	cout<<"? "<<t<<"\n";
	fflush(stdout);
	int req;  cin>>req;
	
	cout<<"! ";
	if(req==num1+1)cout<<L<<" "<<R<<"\n";
	else cout<<R<<" "<<L<<"\n";
	fflush(stdout);
	return 0;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页