「CZOI-R7」括号 题解
Cifera_meow · · 题解
很有意思的一道题,赛时花了近一个小时,罚时吃饱了()
首先我们来看一下题目中的
接下来考虑辨别这是左括号还是右括号,我们询问区间
那么我们可以考虑判断
假设与
那么考虑上询问返回最小值的性质,以及数据范围中有点奇怪的
根据上述探讨,在
至此,你就可以得到
我们发现若
以奇偶性为始,亦以奇偶性为终,一道有意思的题目,不是吗?
#include<bits/stdc++.h>
using namespace std;
int n,p,c,l,r,mid,typ,F,ans;
int ask(int a,int b){
printf("? %d %d",a,b);
cout << endl;
int k;
scanf("%d",&k);
return k;
}
int main(){
scanf("%d%d",&n,&p);
if(p == 1)c = 1; //这是对subtask2的特判(p=1则p一定是左括号),删去不影响通过
else c = ask(p,p);
if((p-c)%2 == 1){
l = 1;
r = p-1;
typ = 1;
}
else {
l = p+1;
r = n*2;
typ = 2;
}
while(l+1 < r){
if(typ == 1){
mid = (l+r+1)/2;
F = ask(mid,p-1);
if(F > c)r = mid-1;
else l=mid;
ans = r;
}
if(typ == 2){
mid = (l+r)/2;
F = ask(p+1,mid);
if(F > c)l = mid+1;
else r=mid;
ans = l;
}
}
if((ans+p)%2 == 0){//由于pq间长度应为偶数,固在区间长度为2时判断ans的值是否符合要求,不符合则做对应调整
if(typ == 1)ans--;
if(typ == 2)ans++;
}
printf("! %d",ans);
cout << endl;
return 0;
}