Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]
Posted Meternal
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]相关的知识,希望对你有一定的参考价值。
题目:http://codeforces.com/problemset/problem/862/D
题意:交互题,询问15次以内Hamming distance,输出一个二进制串里任意一个0或1的位置
题解:极简单的二分,从最后一位先判断一个,然后二分 根据上次和本次的距离差是否等于二分长度判断在左端还是右端有需要寻找的值寻找另一个。
1 #define _CRT_SECURE_NO_DEPRECATE 2 #pragma comment(linker, "/STACK:102400000,102400000") 3 #include<iostream> 4 #include<cstdio> 5 #include<fstream> 6 #include<iomanip> 7 #include<algorithm> 8 #include<cmath> 9 #include<deque> 10 #include<vector> 11 #include<bitset> 12 #include<queue> 13 #include<string> 14 #include<cstring> 15 #include<map> 16 #include<stack> 17 #include<set> 18 #include<functional> 19 #define pii pair<int, int> 20 #define mod 1000000007 21 #define mp make_pair 22 #define pi acos(-1) 23 #define eps 0.00000001 24 #define mst(a,i) memset(a,i,sizeof(a)) 25 #define all(n) n.begin(),n.end() 26 #define lson(x) ((x<<1)) 27 #define rson(x) ((x<<1)|1) 28 #define inf 0x3f3f3f3f 29 typedef long long ll; 30 typedef unsigned long long ull; 31 using namespace std; 32 const int maxn = 1e3 + 5; 33 34 string a; 35 int ans0, ans1; 36 int main() 37 { 38 ios::sync_with_stdio(false); 39 cin.tie(0); cout.tie(0); 40 int i, j, k, m, n; 41 cin >> n; 42 int mode; 43 a.append(n, \'0\'); 44 int cas = 0, test; 45 cout << "? " << a << endl; 46 cout.flush(); 47 cin >> cas; 48 a[n - 1] = \'1\'; 49 cout << "? " << a << endl; 50 cout.flush(); 51 cin >> test; 52 if (test == cas + 1) { ans0 = n; mode = 1; } 53 else { ans1 = n; mode = 0; } 54 if (mode == 0) { cas = n - cas; for (int i = 0; i < n; ++i)a[i] = \'1\'; } 55 else cas = test; 56 int l = 0, r = n - 2; 57 while (r - l > 1) 58 { 59 int mid = l + r >> 1; 60 for (int i = l; i <= mid; ++i)a[i] = mode + \'0\'; 61 cout << "? " << a << endl; 62 cout.flush(); 63 cin >> test; 64 int len = mid - l + 1; 65 if (test - len == cas) { l = mid + 1; cas = test; } 66 else { for (int i = l; i <= mid; ++i)a[i] = (mode ^ 1) + \'0\'; r = mid; } 67 } 68 a[l] = mode + \'0\'; 69 cout << "? " << a << endl; 70 cout.flush(); 71 cin >> test; 72 if (test == cas - 1) 73 { 74 if (mode == 1)ans1 = l+1; 75 else ans0 = l+1; 76 } 77 else 78 { 79 if (mode == 1)ans1 = r+1; 80 else ans0 = r+1; 81 } 82 cout << "! " << ans0 << " " << ans1 << endl; 83 cout.flush(); 84 return 0; 85 }
以上是关于Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]的主要内容,如果未能解决你的问题,请参考以下文章
D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)
Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]
Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function(预处理+二分)
Codeforces Round #436 E. Fire(背包dp+输出路径)