题目链接:http://codeforces.com/contest/862/submission/30696399
题解:这题一看操作数就知道是二分答案了。然后就是怎么个二分法,有两种思路第一种是找两遍第一遍找1第二遍找0但是这样肯定超时所以还不如直接找相邻的01串或者10串具体查找方法是先将所有串赋值为0这样就能得到1的总个数,然后将所要求的区间全赋值为1其他赋值为0。设num1为总的1的个数反馈的值其实就是表示L~R之间0的个数,然后只要满足(设返回的数为now)只要满足num1-now<(R-L+1)&&num1-now>-(R-L+1)就二分到这个区间因为这个区间能够确保既有0又有1然后知道L与R就相差1就行,之后确定0,1位置就简单了就是在L,R两个位置里找,还有题目要求的flush一下
#include <iostream> #include <cstring> #include <cstdio> using namespace std; string s; int now , num1 , n; bool check(int L , int R) { cout << "? "; for(int i = 0 ; i < L ; i++) putchar('0'); for(int i = L ; i <= R ; i++) putchar('1'); for(int i = R + 1 ; i < n ; i++) putchar('0'); puts(""); fflush(stdout); cin >> now; if(num1 - now > -(R - L + 1) && num1 - now < (R - L + 1)) return true; else return false; } int main() { cin >> n; cout << "? "; for(int i = 0 ; i < n ; i++) { putchar('0'); } puts(""); fflush(stdout); cin >> num1; int L = 0 , R = n - 1; while(R - L > 1) { int mid = (L + R) >> 1; if(check(mid , R)) { L = mid; } else { R = mid; } } cout << "? "; for(int i = 0 ; i < L ; i++) putchar('0'); for(int i = L ; i < R ; i++) putchar('1'); for(int i = R ; i < n ; i++) putchar('0'); puts(""); fflush(stdout); cin >> now; if(now > num1) { cout << "! " << L + 1 << ' ' << R + 1 << endl; } else { cout << "! " << R + 1 << ' ' << L + 1 << endl; } return 0; }