1. 程式人生 > >Codeforces 862D. Mahmoud and Ehab and the binary string (二分)

Codeforces 862D. Mahmoud and Ehab and the binary string (二分)

num 找到 不同的 IT str href targe 不同 一個

題目鏈接:Mahmoud and Ehab and the binary string

題意:

  一道交互題,首先給出一個字符串的長度l。現在讓你進行提問(最多15次),每次提問提出一個字符串,會返回這個字符串與目標串之間不同的字符的個數。要在15次的提問內,找到目標串中任意一個1和0的位置。

題解:

  因為是15次提問,其實可以想到二分,但是分別對0和1進行二分差不多要二十多次,所以可以對01或10進行二分,查找某一個01或10串的位置。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int MAX_N = 1e5+9
; 4 int vec[MAX_N]; 5 int main() 6 { 7 int N,M,T; 8 cin>>N; 9 printf("?\n"); 10 for(int i=1;i<=N;i++) printf("0"); 11 cout<<endl; 12 int num; 13 cin>>num; 14 int l = 1,r = N,x; 15 while(r-l>=2) 16 { 17 int mid = (l+r)/2; 18 printf("
?\n"); 19 for(int i=1;i<=N;i++) 20 { 21 if(i>=l && i<=mid) printf("1"); 22 else printf("0"); 23 } 24 cout<<endl; 25 cin>>x; 26 if(abs(num - x) == mid - l + 1) l = mid; 27 else r = mid; 28 }
29 int x1,x2; 30 printf("?\n"); 31 for(int i=1;i<=N;i++) 32 { 33 if(i == l) printf("1"); 34 else printf("0"); 35 } 36 cout<<endl; 37 cin>>x1; 38 if(x1<num) 39 { 40 printf("! %d %d\n",r,l); 41 } 42 else printf("! %d %d\n",l,r); 43 return 0; 44 }

Codeforces 862D. Mahmoud and Ehab and the binary string (二分)