题解:P14320 「ALFR Round 11」C2 开关灯 (switch) (hd ver.)

· · 题解

好做好想在哪了?好做好想在哪了?好做好想在哪了?

ez ver. 的题解通道关了相信大家都会了,所以就不细讲了。

大概就是反转后如果出现破绽那么二分确定位置,没有破绽再翻一次肯定也就有了。

重要前提:二分确定 [1,500] 中的一个数字最坏要 9 次,而 [1,250] 则是 8 次。

你怎么知道我赛时掰手指数数结果还是把 [1,500] 数成了 8,最后 22 数成了 21 于是兴高采烈的写了个错解下去,警钟长鸣。

考虑沿用策略。

我们整体反转一次,然后检测破绽数量,分类讨论。

如果一次性把两个都炸出来了,直接开始二分,找到一个点使得区间可以分成两半且左右各一个目标后,再拆开来跑正常二分。

最坏操作 19 次,可解。

如果一个出来的都没有,再反转一遍,两个就一定都出来了。

那么就是上一种情况多用一步反转,最坏 20 次,可解。

炸出来一个,二分找到这个灯。

目前耗费 11 步。

再反转。

此时原先没露出破绽的必然会有锅,但是原本露出破绽的那个情况却是不可控的,于是继续分类讨论。

只剩一个,继续二分。

共计耗费步数:\red{22} 步。

剩两个。

我们在二分检测的时候如果发现询问区间内包含第一遍找到的灯泡,直接减掉这个的影响,于是一样可以正常二分。

共计耗费步数:\red{22} 步。

没错上面两个标红的数字我赛时数错了。

大力分讨实现,可以获得 95 分。

赛时代码:

#include<bits/stdc++.h>
using namespace std;
void solve(){
    int ret1,ret2;
    int n;
    cin>>n;
    cout<<"1 1 "<<n<<endl;
    int x;
    cout<<"2 1 "<<n<<endl;
    cin>>x;
    x=n-x;
    if(x==2){
        int l=1,r=n,mid;
        while(l<r){
            mid=(l+r)>>1;
            cout<<"2 "<<l<<' '<<mid<<endl;
            cin>>x;
            x=mid-l+1-x;
            if(x==2)r=mid;
            if(x==1)break;
            if(x==0)l=mid+1;
        }
        int L,R;
        L=l,R=mid;
        while(L<R){
            int md=(L+R)>>1;
            cout<<"2 "<<L<<' '<<md<<endl;
            cin>>x;
            x=md-L+1-x;
            if(x)R=md;
            else L=md+1;
        }
        ret1=R;
        L=mid+1,R=r;
        while(L<R){
            int md=(L+R)>>1;
            cout<<"2 "<<L<<' '<<md<<endl;
            cin>>x;
            x=md-L+1-x;
            if(x)R=md;
            else L=md+1;
        }
        ret2=R;
        cout<<"3 "<<ret1<<' '<<ret2<<endl;
        return;
    }
    if(x==1){
        int L,R;
        L=1,R=n;
        while(L<R){
            int md=(L+R)>>1;
            cout<<"2 "<<L<<' '<<md<<endl;
            cin>>x;
            x=md-L+1-x;
            if(x)R=md;
            else L=md+1;
        }
        ret1=R;
        cout<<"1 1 "<<n<<endl;
        int op;
        cout<<"2 1 "<<n<<endl;
        cin>>op;
        if(op==1){
            L=1,R=n;
            while(L<R){
                int md=(L+R)>>1;
                cout<<"2 "<<L<<' '<<md<<endl;
                cin>>x;
                if(x)R=md;
                else L=md+1;
            }
            ret2=R;
        }else{
            L=1,R=n;
            while(L<R){
                int md=(L+R)>>1;
                cout<<"2 "<<L<<' '<<md<<endl;
                cin>>x;
                x-=L<=ret1&&ret1<=md;
                if(x)R=md;
                else L=md+1;
            }
            ret2=R;
        }
        cout<<"3 "<<ret1<<' '<<ret2<<endl;
        return;
    }
    cout<<"1 1 "<<n<<endl;
    int l=1,r=n,mid;
    while(l<r){
        mid=(l+r)>>1;
        cout<<"2 "<<l<<' '<<mid<<endl;
        cin>>x;
        if(x==2)r=mid;
        if(x==1)break;
        if(x==0)l=mid+1;
    }
    int L,R;
    L=l,R=mid;
    while(L<R){
        int md=(L+R)>>1;
        cout<<"2 "<<L<<' '<<md<<endl;
        cin>>x;
        if(x)R=md;
        else L=md+1;
    }
    ret1=R;
    L=mid+1,R=r;
    while(L<R){
        int md=(L+R)>>1;
        cout<<"2 "<<L<<' '<<md<<endl;
        cin>>x;
        if(x)R=md;
        else L=md+1;
    }
    ret2=R;
    cout<<"3 "<<ret1<<' '<<ret2<<endl;
}
int main(){
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}
//「一起怀抱梦想吧,纳莎妮亚。就当作是代替你已经破灭的梦想也好。」
//「有朝一日,一起创造出属于我们家族的国家吧。」
// 在宛如修缮得不够彻底的家畜棚屋一般,有点脏的房间里。

//「希望你能跟缇亚忒好好相处。」
//「请你把她当成一个女孩子来对待,让她能活得像个女孩子。」
// 在下著冷雨的街上,休息处的屋檐下面。

//「我再也没办法对妖精族的未来抱持希望了!」
//「连那些孩子都要被污染的未来,我连想都不愿想!」
// 在以熊熊燃烧的飞空艇残骸为背景的情况下。

如何优化?

注意到后面两种不合法情况的分类后的处理是相似的,考虑有没有办法合并,这样分类时的检验这一步就可以压掉。

我们充分发扬人类智慧,运用构造直觉,注意到二分时一定有一半是不包含上一个已验明的点的。

那我们对于这半边进行检测,如果有结果那显然在这半边,否则是和已验明的点同侧。

诶,那这个不就不需要考虑已验明的点在反转后的状态了啊!

于是写一个很诡异的二分代替那一坨分讨就做完了。

操作次数卡线。

#include<bits/stdc++.h>
using namespace std;
void solve(){
    int ret1,ret2;
    int n;
    cin>>n;
    cout<<"1 1 "<<n<<endl;
    int x;
    cout<<"2 1 "<<n<<endl;
    cin>>x;
    x=n-x;
    if(x==2){
        int l=1,r=n,mid;
        while(l<r){
            mid=(l+r)>>1;
            cout<<"2 "<<l<<' '<<mid<<endl;
            cin>>x;
            x=mid-l+1-x;
            if(x==2)r=mid;
            if(x==1)break;
            if(x==0)l=mid+1;
        }
        int L,R;
        L=l,R=mid;
        while(L<R){
            int md=(L+R)>>1;
            cout<<"2 "<<L<<' '<<md<<endl;
            cin>>x;
            x=md-L+1-x;
            if(x)R=md;
            else L=md+1;
        }
        ret1=R;
        L=mid+1,R=r;
        while(L<R){
            int md=(L+R)>>1;
            cout<<"2 "<<L<<' '<<md<<endl;
            cin>>x;
            x=md-L+1-x;
            if(x)R=md;
            else L=md+1;
        }
        ret2=R;
        cout<<"3 "<<ret1<<' '<<ret2<<endl;
        return;
    }
    if(x==1){
        int L,R;
        L=1,R=n;
        while(L<R){
            int md=(L+R)>>1;
            cout<<"2 "<<L<<' '<<md<<endl;
            cin>>x;
            x=md-L+1-x;
            if(x)R=md;
            else L=md+1;
        }
        ret1=R;
        cout<<"1 1 "<<n<<endl;
        L=1,R=n;
        while(L<R&&L<=ret1&&ret1<=R){
            int md=(L+R)>>1;
            bool op=(ret1>md);
            if(op)
            cout<<"2 "<<L<<' '<<md<<endl;
            else
            cout<<"2 "<<md+1<<' '<<R<<endl;
            cin>>x;
            if(op){
                if(x)R=md;
                else L=md+1;
            }else{
                if(x)L=md+1;
                else R=md;
            }
        }
        while(L<R){
            int md=(L+R)>>1;
            cout<<"2 "<<L<<' '<<md<<endl;
            cin>>x;
            if(x)R=md;
            else L=md+1;
        }
        ret2=R;
        cout<<"3 "<<ret1<<' '<<ret2<<endl;
        return;
    }
    cout<<"1 1 "<<n<<endl;
    int l=1,r=n,mid;
    while(l<r){
        mid=(l+r)>>1;
        cout<<"2 "<<l<<' '<<mid<<endl;
        cin>>x;
        if(x==2)r=mid;
        if(x==1)break;
        if(x==0)l=mid+1;
    }
    int L,R;
    L=l,R=mid;
    while(L<R){
        int md=(L+R)>>1;
        cout<<"2 "<<L<<' '<<md<<endl;
        cin>>x;
        if(x)R=md;
        else L=md+1;
    }
    ret1=R;
    L=mid+1,R=r;
    while(L<R){
        int md=(L+R)>>1;
        cout<<"2 "<<L<<' '<<md<<endl;
        cin>>x;
        if(x)R=md;
        else L=md+1;
    }
    ret2=R;
    cout<<"3 "<<ret1<<' '<<ret2<<endl;
}
int main(){
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}
//「我很怕大家都不见。我讨厌害怕的感觉。」
//「不过,我也不希望自己变成发生任何事都无动于衷的人。」
// 在百花齐放的原野,做著要送给朋友的花冠之际。

//「我没事的。我一个人也没问题。我照样能幸福。」
// 在因为祭典而喧闹不已的夜晚街道上,观赏迈入高潮的人偶剧之际。

//「在这世上,有的夫妻也是婚后才开始培养感情的吧?」
//「至少现在的我,对于这样的心境感到很幸福。」
// 在狭窄的个人施疗院的一处室内,与大块头医师面对面的情况下。

// Lakhesh Nyx Seniorious