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

http://codeforces.com/contest/862/problem/D

询问题

fflush(stdout)

调试:

先行给出结果,函数代替输入

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <time.h>
  6 #include <string>
  7 #include <set>
  8 #include <map>
  9 #include <list>
 10 #include <stack>
 11 #include <queue>
 12 #include <vector>
 13 #include <bitset>
 14 #include <ext/rope>
 15 #include <algorithm>
 16 #include <iostream>
 17 using namespace std;
 18 #define ll long long
 19 #define minv 1e-6
 20 #define inf 1e9
 21 #define pi 3.1415926536
 22 #define E  2.7182818284
 23 const ll mod=1e9+7;//998244353
 24 const int maxn=1e3+10;
 25
 26 char str[maxn];
 27 int pos[2],s,t,len=inf,x,y;
 28 char result[maxn]="01\0";
 29 int nn=2;
 30
 31 int cal()
 32 {
 33     int i,v=0;
 34     for (i=1;i<=nn;i++)
 35         if (str[i]!=result[i-1])
 36             v++;
 37     return v;
 38 }
 39
 40 //‘l‘-‘r‘ must have num
 41 void work(int l,int r,int num)
 42 {
 43     if (l==r)
 44     {
 45         pos[num]=l;
 46         return;
 47     }
 48
 49     int i,m=(l+r)>>1;
 50     for (i=l;i<=m;i++)
 51         str[i]=((str[i]-48) ^ 1)+48;
 52     printf("? %s\n",str+1);
 53     fflush(stdout);
 54     scanf("%d",&x);
 55 //        x=cal();
 56
 57     for (i=l;i<=m;i++)
 58         str[i]=((str[i]-48) ^ 1)+48;
 59
 60     if (y-x==m-l+1)
 61     {
 62         pos[num]=l;
 63         return;
 64     }
 65     else
 66     {
 67         if (m-l+1<len)
 68         {
 69             len=m-l+1;
 70             s=l;
 71             t=m;
 72         }
 73     }
 74
 75     if (x-y!=m-l+1)
 76         work(l,m,num);
 77     else
 78         work(m+1,r,num);
 79 }
 80
 81 int main()
 82 {
 83     int n,i;
 84     scanf("%d",&n);
 85
 86     for (i=1;i<=n;i++)
 87         str[i]=‘1‘;
 88     str[n+1]=‘\0‘;
 89     printf("? %s\n",str+1);
 90     fflush(stdout);
 91     scanf("%d",&y);
 92 //        y=cal();
 93     work(1,n,0);
 94
 95     if (len>0)
 96     {
 97         for (i=1;i<=n;i++)
 98             str[i]=‘0‘;
 99         str[n+1]=‘\0‘;
100         printf("? %s\n",str+1);
101         fflush(stdout);
102         scanf("%d",&y);
103 //            y=cal();
104         if (len==inf)
105             s=1,t=n;
106         work(s,t,1);
107     }
108
109     printf("! %d %d",pos[0],pos[1]);
110     return 0;
111 }

原文地址:https://www.cnblogs.com/cmyg/p/9521040.html

时间: 2024-08-27 02:45:05

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

题目:http://codeforces.com/problemset/problem/862/D 题意:交互题,询问15次以内Hamming distance,输出一个二进制串里任意一个0或1的位置 题解:极简单的二分,从最后一位先判断一个,然后二分 根据上次和本次的距离差是否等于二分长度判断在左端还是右端有需要寻找的值寻找另一个. 1 #define _CRT_SECURE_NO_DEPRECATE 2 #pragma comment(linker, "/STACK:102400000,10

Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互题)

<题目链接> 题目大意: 有一个长度为n(n<1000)的01串,该串中至少有一个0和一个1,现在由你构造出一些01串,进行询问,然后系统会给出你构造的串与原串的   Hamming distance ,现在要求你按照步骤进行交互式操作,最终得到任意一个0.1的下标. 解题分析:因为原串中至少存在一个0和一个1,所以一定存在一个01或者10序列,因此我们可以用二分来寻找这个序列(注意二分过程中选择区间的操作).二分之后,一定能够得到01或10序列,然后将其按先0后1的顺序输出即可. 1

E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)

http://codeforces.com/contest/862/problem/E 二分答案 一个数与数组中的哪个数最接近: 先对数组中的数排序,然后lower_bound 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #includ

Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function(预处理+二分)

题目链接:点我点我 题意:公式:,给出n个数,从a[1]到a[n],和m个数(b数组),然后从b数组里挑出连续的n个数(也就m-n+1中选择),按公式计算,使得f最小, 还有q次对a数组的操作(某个区间增加值,减少值),求出最小值. 题解:显然对a数组的处理非常简单,一开始确定一定值,然后update的时候,判断一下奇偶性质就可以直接加了(前一项正后一项一定是负的,可以抵消). 然后就是b数组的处理了,一开始没处理好,一直在这边卡超时.先把b数组一项一项正负不同的加进去,然后再进行处理,得到c数

C Good String Codeforces Round #560 (Div. 3)

Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on).

UVA - 10559 Blocks 和 Vasya and Binary String CodeForces - 1107E (dp OR 记忆化搜索)

UVA - 10559 Blocks 题意:消消乐,每次连续相同的可以消除,分数加上长度的平方,问最多可以获得几分全部消完 题解: 区间dp + 记忆化搜索 dp[i][j][k] : (区间 [i,  j] 后面带上一段和 j 颜色相同的且长度为 k )的消消乐最大积分 1.消最后一段颜色和 j 颜色相同的 dp[i][j][k] <-- dp[i][j-1][0] + (k+1)^2 2.对于i <= l < j, 如果 l 和 j 的颜色相同, 那么可以把 [l+1, j-1]消掉

Codeforces Round #598 (Div. 3) D - Binary String Minimizing

原文链接:https://www.cnblogs.com/xwl3109377858/p/11797618.html Codeforces Round #598 (Div. 3) D - Binary String Minimizing You are given a binary string of length n (i. e. a string consisting of n characters '0' and '1'). In one move you can swap two adj

Codeforces Round #396 (Div. 2) D题Mahmoud and a Dictionary(并查集)解题报告

Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discov

贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

题目传送门 1 /* 2 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 3 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 #include <iostream> 10 using namespace std; 11 12 const int MAX