题解:CF1698D Fixed Point Guessing

· · 题解

CF1698D 题解

看到 \log_2 10^4 \approx 15 ,考虑二分。

以下令 c_x 表示 x 交换的数。(换种方法说就是 x 与 c_x 交换了),若这个数没有交换则 c_x = 0。

考虑询问区间 [l,r] 与答案的关系。

对于一个数 x \in [l,r],如果 c_x \neq 0,有且仅有两种可能:

但当 c_x = 0 时,区间 [l,r] 中会多出来一个匹配的。也就是说,当前匹配的数的数量与 [l,r] 的长度奇偶性不同。

也就是说,当且仅当询问 [l,r] 区间得到的序列中,在 [l,r] 中的数与 r-l+1 奇偶不同时,没有被交换的数在这个区间里。

接下来直接二分就行了。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int l=1,r=n;
        while(l<r){
            int mid=(l+r)/2;
            int q=mid-l+1;//区间长度
            int acc=0;
            cout<<"? "<<l<<" "<<mid<<endl;
            int x;
            for(int i=1;i<=q;i++){
                cin>>x;
                if(x<=mid&&x>=l)acc++;
            }
            if(acc%2==0)l=mid+1;
            else r=mid;
        }
        cout<<"! "<<l<<endl;
    }
}