1. 程式人生 > >Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]

Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]

cstring stream ++ else fine eps comm 簡單的 hab

題目: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[二分]