跳到正文

题解

题解:CF1698D Fixed Point Guessing

zxh_qwq约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年4月7日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 2 分钟
题目
CF1698D

版本

  1. v1当前

CF1698D 题解

看到 log⁡2104≈15\log_2 10^4 \approx 15 ,考虑二分。

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

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

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

  1. cx∈[l,r]c_x \in [l,r]。此时区间 [l,r][l,r] 中,升序排列后数字不变。

  2. cx∉[l,r]c_x \notin [l,r]。此时区间 [l,r][l,r] 中会多出来一个数 cxc_x,同时也会少一个数 xx。此时区间 [l,r][l,r] 中的数字与询问得到的数字会有两个不匹配的。也就是说,当前匹配的数的数量与 [l,r][l,r] 的长度同奇偶。

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

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

接下来直接二分就行了。

代码

C++
#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;
    }
}
原文