题解:CF2209C Find the Zero
题目大意
一道交互题。
给出一个长度为
你可以进行
你需要确定至少
思路
算法 0
随机化。
但是交互库是适应性的,那没办法了。
性质 1
我们发现如果两个数
性质 2
因为提问次数大约为
性质 3
如果将
我们使用鸽笼原理,易得每组中两个数必定有一个为
算法 1
有了性质
那如果全部输出
询问
反思算法 1
为什么仍然需要询问
我们先像算法
但是,我们发现另一个很好的性质:如果前
那么我们仍然可以像算法一一样比较
询问
code
#include <bits/stdc++.h>
using namespace std;
int T;
int n;
bool flag;
int main() {
scanf("%d", &T);
while (T--) {
flag = 0;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
printf("? %d %d\n", 2 * i - 1, 2 * i);
fflush(stdout);
int res;
scanf("%d", &res);
if (res) {
printf("! %d\n", 2 * i);
fflush(stdout);
flag = 1;
break;
}
}
if (flag) {
continue;
}
printf("? %d %d\n", 1, 2*n-1);
fflush(stdout);
int res;
scanf("%d", &res);
if(res){
puts("! 1");
fflush(stdout);
continue;
}
printf("? %d %d\n", 2, 2*n-1);
fflush(stdout);
scanf("%d", &res);
if(res){
puts("! 2");
fflush(stdout);
continue;
}
printf("! %d\n",2*n);
fflush(stdout);
}
return 0;
}