题解 CF1999G2 Ruler (hard version)
cjh20090318 · · 题解
很简单的交互题,不怎么需要动脑子。
题意
这是一道交互题。
和简单版本不同的是,你最多可以进行
有一个秘密的尺子,上面缺少一个数字
- 如果
y < x ,尺子(正确地)测量物体长度为y 。 - 如果
y \ge x ,尺子错误地测量物体长度为y+1 。
你需要找出
找出
分析
每次可以询问两个值,如果二分同一个值的话未免显得有点浪费。
答案具有单调性,且
初始时
查询
最后得到的
代码
//the code is from chenjh
#include<cstdio>
#define FLUSH fflush(stdout)
using namespace std;
int check(const int x,const int y){
printf("? %d %d\n",x,y),FLUSH;
int ret;scanf("%d",&ret);
return ret;
}
void solve(){
int l=2,r=999;
for(int m1,m2,ret;l<r;){
m1=l+(r-l)/3,m2=r-(r-l)/3;
ret=check(m1,m2);
if(ret==(m1+1)*(m2+1)) r=m1;
else if(ret==m1*(m2+1)) l=m1+1,r=m2;
else l=m2+1;
}
printf("! %d\n",l),FLUSH;
}
int main(){
int T;scanf("%d",&T);
while(T--) solve();
return 0;
}