cf1064E. Dwarves, Hats and Extrasensory Abilities(二分 交互)

时间:2024-06-21 09:07:14

题意

题目链接

\(n\)次操作,每次你给出一个点的坐标,系统会返回该点的颜色(黑 / 白),程序最后输出一条直线把所有黑点和白点分隔开

Sol

一个很直观的想法:首先询问\((dx, 0)\),然后每次询问二分中点,根据与第一次询问得到的字符串的关系不断调整二分范围

但是这样会被卡,我修改了两个地方才过。

  1. 二分调整边界的时候直接设\(l = mid\)或\(r = mid\),因为我们最后得到的不是一个精确解,所以这样写是可以的

  2. 最后输出直线的时候加一个偏移量,也就是输出一条斜线

具体看代码

/*
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long
#define ull unsigned long long
#define rg register
#define pt(x) printf("%d ", x);
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
//char obuf[1<<24], *O = obuf;
//void print(int x) {if(x > 9) print(x / 10); *O++ = x % 10 + '0';}
//#define OS *O++ = ' ';
//#define fout fwrite(obuf, O-obuf, 1 , stdout);
using namespace std;
//using namespace __gnu_pbds;
const int MAXN = 2005, INF = 1e9 + 10, mod = 1e9 + 7;
const int D[] = { -1, 1};
const double eps = 1e-9;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
} int N, Dx = 23333;
string s, pre; main() {
N = read();
int l = 0, r = 1e9;
printf("%d 0\n", Dx);
fflush(stdout);
cin >> pre;
int ans = 0;
for(int i = 2; i <= N; i++) {
int mid = l + r >> 1;
printf("%d %d\n", Dx, mid);
fflush(stdout);
cin >> s;
if(s != pre) r = mid;
else l = mid, ans = mid;
} printf("%d %d %d %d", Dx - 3, ans, Dx + 3, ans + 1);
return 0;
}
/*
5
black
black
white
white
black
*/