[eJOI 2026] Elevator 题解

· · 题解

鱼生第二道通信题,考察信息的传递和损耗,是好玩的。

给出一种本质相同、实现方式和细节不太相同的思路。

题目中给出的各个子任务都有不同约束,因此我们考虑逐个完成。

子任务 1

保证 0 \le v_i \le 1,且每个 0 后必有一个 1,要求还原 60/60 个居民的秘密数字。

考虑到如果需要还原每个楼层的数字,则必须让所有有效楼层的居民都能操作电梯。

但是一巴掌按下所有按钮显然不行。我们考虑到秘密数字为 0 的楼层后面的 1 就是一条无效信息;只要确定所有 0 的位置就能确定所有 1 的位置。倒着看,能确定所有 0 后的 1 的位置,就一定可以获取 0 的位置。

这不难做到。对于每个居民,可以执行如下操作:

还原时,所有 0 后面的 1 都被不重不漏地跳过了。

因此:所有空按钮的下面一层必然是 0,其它楼层均为 1。

这样就做完了。

子任务 2

保证 0 \le v_i \le 1,要求还原 40/60 个居民的秘密数字。

也就是每 3 个居民中就要还原两个。

我们尝试将所有居民三个三个捆绑,每组中都出现 2 位可以操作电梯并传递信息的居民。

不难想到,信息传递的方法可以依赖于操作下组居民的电梯按钮。

那么,一次合法的信息传递需要:

现在问题即转化为,如何安排下一组的比特位,使得满足上述两个条件。

不难发现,如果 3 个比特位中 1 的个数要 \ge 2,则可能的方案数有 \binom{3}{2}+\binom{3}{3}=4 种,而可能的两个神秘数字的状态也是 2^2=4 种,两者恰好相等。

问题在于,两位居民可能不知道彼此的神秘数字,因此该做法不能仅停留于理论计算。

我们考虑这个过程:第一位居民先通过按下电梯的某一位,来“通知”另外一位居民自己的神秘数字;另外一位居民通过解码和拼装两个居民的所有信息,在电梯按钮上补按按钮,完成所有过程。

而这个构造方案不难,我来随便构造一组:

特殊地,为了让每组的贡献都恰为 2,如果一组中出现了 111,则选一位令其不操作电梯按钮。

这样就能完成子任务 2 了。

子任务 3

保证 0 \le v_i \le 2,要求还原 30/60 个居民的秘密数字。

也就是每 2 个居民中要还原一个。

我们延续子任务 2 的想法,将整个电梯楼层剖成 30 组,每组两个人。

考虑每组只传递一个信息,该信息有 3 种可能性。

为了让下一组能出人,我们将这些可能性一一映射到按钮状态的 01、10、11 上面。

然后你发现组内只有一个人,甚至不用信息传递,直接就做完了,比子任务 2 还简单。

子任务 4...?

保证 0 \le v_i \le 3,要求还原 25/60 个居民的秘密数字。

也就是每 12 个居民里要还原 5 个。

我们继续延续子任务 2 的想法,将整个电梯楼层剖成 5 组,每组 12 个人。

考虑每组只传递五条信息,这些信息的可能性数量一共有 4^{5}=1024 种。

为了让下一组能出至少 5 个人,所以可能的比特串的个数就是:

\sum_{i=5}^{12} \binom{12}{i}

不难算出结果为 3301,这个值远大于 1024,那还不简单,直接做完了...吗?

并没有。在上文中我们提到,居民们可能不知道彼此的神秘数字,因此该做法不能仅停留于理论计算。

问题在于,每个居民只能在已经按下的按钮的基础上继续添加按钮,但绝对不能抹除按钮。

更致命的是,一组的居民只能通过之前的同组居民所按按钮判断它们的秘密数字。因此前面的信息传输也必须到位。

我们举一个例子。根据既往的构造方法,为了让下一组的一些比特和这一组的秘密数字状态之间产生尽量多的双射,第一个居民的最优安排是根据自己的神秘数字在前 4 位中选择一位按下。单是这一步,就注定了最终电梯序列的那四个电梯按钮中,一定会有一个亮起。算上接下来的居民传输,这会排除不少状态数。

因此,问题在于能否找到与信息量恰好匹配的紧凑在线编码。

十二个元素分组的构造相对困难,并且构造出来分支太多也比较吃力,我们于是弃用这个做法。

子任务 4

我们尝试找到一种相对紧凑的构建方法。

五个居民太难传递,一个居民的状态数正好占满两个比特位且可能都为 0,这些都很难传递。

我们考虑两个居民。

注意到:两个居民的秘密数字的可能状态个数是 16 个,这个值恰好等于 \binom{5}{3}+\binom{5}{4}+\binom{5}{5}。

我们仍将居民分组,每组 5 人,设这样的组数为 k。

则这些居民能够创造的可还原个数就是 2k,并且保证了至少多按下 k+1 个不参与组间信息传递的按钮。

这些按钮可以在剩余的比特位上面传递自己的信息,每两位构成的二进制数可以和神秘数字双射。

考虑按钮 1 固定被按下,增多的可还原个数即为 \min \{ k+1,29.5-2.5k \}。

现在只需要让 2k+\min \{ k+1,29.5-2.5k \} 的值尽量大,它的图像大概长这个样子。

这个时候取 k=8 或者 k=9 都可以得到 25,正确性没锅,然后构造一下就做完了。

下面给出一组构造:

A 的数 \ B 的数 0 1 2 3
0 11100 11010 11001 10110
1 01011 01101 01110 01111
2 10101 11110 11101 10111
3 00111 11011 10011 11111

这个限制是真的一点都不放宽啊,做这题真的有点刀尖起舞的感觉。

上述所有思路在精细实现后,可以解决此题。

最后给出算法的一张图解:

代码给出,部分实现不再花篇幅讲解,如遇细节问题可私信指正。

#include<bits/stdc++.h>
// #include"elevator.h"
using namespace std;
inline bool nexist(vector<int> &p,int x){ return find(p.begin(),p.end(),x)==p.end(); }
inline void press(vector<int> &p,vector<int> &res,int x,int f,int N){
    if(x>f and x<=N and nexist(p,x)) res.push_back(x);
}
vector<int> press_buttons(int subtask,int N,int f,int v,vector<int> p){
    vector<int> res; res.clear();
    if(subtask<=1) press(p,res,((v==0)?(f+2):(f+1)),f,N);
    else if(subtask==2){
        int cur=(f+1)/3+1;
        int neA=cur*3-1, neB=neA+1, neC=neB+1;
        if(f!=1 and f%3==1 and !nexist(p,f-1) and !nexist(p,f-2)) return res; // 排除一种特殊情况:该组被同时按下三个 1
        if(f==59 or f==60) return res;
        if(f>=56){
            // 第一种情况:处理 59
            if(f==56 or (f==57 and nexist(p,56))){ if(v) press(p,res,59,f,N); }
            // 第二种情况:处理 60
            else{ if(v) press(p,res,60,f,N); }
            return res;
        }
        // 第一种情况:开创的
        if(nexist(p,neA) and nexist(p,neB)){
            if(v==0) press(p,res,neA,f,N);
            else press(p,res,neB,f,N);
        }
        // 第二种情况:搞衔接的
        else if(nexist(p,neA)){
            press(p,res,neC,f,N);
            if(v==1) press(p,res,neA,f,N);
        }else{
            if(v==0) press(p,res,neB,f,N);
            else press(p,res,neC,f,N);
        }
        if(f==0) press(p,res,1,f,N);
    }else if(subtask==3){
        int cur=(f+1)/2+1; // 当前这一组的编号
        int neA=cur*2-1, neB=neA+1; // 下一组的两个编号
        if(f!=0 and f%2==0 and !nexist(p,f-1)) return res; // 排除一种特殊情况:该组被同时按下两个 1
        if(v==0) press(p,res,neA,f,N);
        else if(v==1) press(p,res,neB,f,N);
        else press(p,res,neA,f,N), press(p,res,neB,f,N);
    }else{ // 请输入文本
        if(f>46) return res;
        if(f==0) press(p,res,1,f,N); 
        int cur=(f+3)/5; // 当前这一组的编号
        if(f!=0 and f%5==0 and !nexist(p,f-1) and !nexist(p,f-2) and !nexist(p,f-3)) return res; // 排除多余位
        if(f!=1 and f%5==1 and (!nexist(p,f-1))+(!nexist(p,f-2))+(!nexist(p,f-3))+(!nexist(p,f-4))>=3) return res; // 排除多余位
        bool cond=0; // 是否是滞留位
        cond|=(f%5==4 and !nexist(p,f-1) and !nexist(p,f-2));
        cond|=(f!=0 and f%5==0 and ((!nexist(p,f-1)+!nexist(p,f-2)+!nexist(p,f-3))==2));
        cond|=(f!=1 and f%5==1 and ((!nexist(p,f-1)+!nexist(p,f-2)+!nexist(p,f-3)+!nexist(p,f-4))==2));
        if(cond){ // 在预留位放置自己的信息
            int putA=45+cur*2, putB=putA+1; // 这一组滞留位应该放置的两个位置
            if(v==2 or v==3) press(p,res,putA,f,N); 
            if(v==1 or v==3) press(p,res,putB,f,N); 
        }else{ // 在下五位放置自己的信息
            int neA=cur*5+2, neB=neA+1, neC=neB+1, neD=neC+1, neE=neD+1; // 这一组位应该放置的五个位置
            if(neA>46) return res;
            // 第一种情况:开创的
            if(nexist(p,neA) and nexist(p,neB) and nexist(p,neC) and nexist(p,neD) and nexist(p,neE)){
                if(v==0) press(p,res,neA,f,N); 
                else if(v==1) press(p,res,neB,f,N); 
                else if(v==2) press(p,res,neC,f,N); 
                else if(v==3) press(p,res,neD,f,N); 
            }
            // 第二种情况:搞衔接的
            else if(!nexist(p,neA)){
                if(v==0) press(p,res,neB,f,N), press(p,res,neC,f,N);
                else if(v==1) press(p,res,neB,f,N), press(p,res,neD,f,N);
                else if(v==2) press(p,res,neB,f,N), press(p,res,neE,f,N);
                else press(p,res,neC,f,N), press(p,res,neD,f,N);
            }else if(!nexist(p,neB)){
                if(v==0) press(p,res,neD,f,N), press(p,res,neE,f,N);
                else if(v==1) press(p,res,neC,f,N), press(p,res,neE,f,N);
                else if(v==2) press(p,res,neC,f,N), press(p,res,neD,f,N);
                else press(p,res,neC,f,N), press(p,res,neD,f,N), press(p,res,neE,f,N);
            }else if(!nexist(p,neC)){
                if(v==0) press(p,res,neA,f,N), press(p,res,neE,f,N);
                else if(v==1) press(p,res,neA,f,N), press(p,res,neB,f,N), press(p,res,neD,f,N);
                else if(v==2) press(p,res,neA,f,N), press(p,res,neB,f,N), press(p,res,neE,f,N);
                else press(p,res,neA,f,N), press(p,res,neD,f,N), press(p,res,neE,f,N);
            }else{
                if(v==0) press(p,res,neC,f,N), press(p,res,neE,f,N);
                else if(v==1) press(p,res,neA,f,N), press(p,res,neB,f,N), press(p,res,neE,f,N);
                else if(v==2) press(p,res,neA,f,N), press(p,res,neE,f,N);
                else press(p,res,neA,f,N), press(p,res,neB,f,N), press(p,res,neC,f,N), press(p,res,neE,f,N);
            }
        }
    }
    return res;
}
vector<int> answer(int subtask,int N,vector<int> p){
    if(subtask<=1){
        vector<int> res(N+1,0);
        for(int i:p) res[i-1]=1;
        res[N]=1;
        return res;
    }else if(subtask==2){
        vector<int> res(N+1,-1);
        int tarA=0, tarB=1; // 当前要填入的两个空格
        for(int i=2; i<=N; i+=3){
            if(i==59){
                res[tarA]=(!nexist(p,59));
                res[tarB]=(!nexist(p,60));
                continue;
            }
            if(nexist(p,i)) res[tarA]=1, res[tarB]=0, tarA=i+1, tarB=i+2;
            else if(nexist(p,i+1)) res[tarA]=0, res[tarB]=1, tarA=i, tarB=i+2;
            else if(nexist(p,i+2)) res[tarA]=0, res[tarB]=0, tarA=i, tarB=i+1;
            else res[tarA]=1, res[tarB]=1, tarA=i, tarB=i+1;
        }
        return res;
    }else if(subtask==3){
        vector<int> res(N+1,-1);
        int tar=0; // 当前要填入的空格
        for(int i=1; i<=N; i+=2){
            if(nexist(p,i)) res[tar]=1, tar=i+1;
            else if(nexist(p,i+1)) res[tar]=0, tar=i;
            else res[tar]=2, tar=i;
        }
        return res;
    }else{ // 请输入文本
        vector<int> res(N+1,-1);
        vector<int> left; // 滞留位
        int tarA=0, tarB=1; // 当前要填入的两个空格
        for(int i=2; i<=42; i+=5){
            bool a=(!nexist(p,i)), b=(!nexist(p,i+1)), c=(!nexist(p,i+2)), d=(!nexist(p,i+3)), e=(!nexist(p,i+4));
            if( a and  b and  c and !d and !e) res[tarA]=0, res[tarB]=0;
            if( a and  b and !c and  d and !e) res[tarA]=0, res[tarB]=1;
            if( a and  b and !c and !d and  e) res[tarA]=0, res[tarB]=2;
            if( a and !b and  c and  d and !e) res[tarA]=0, res[tarB]=3;
            if(!a and  b and !c and  d and  e) res[tarA]=1, res[tarB]=0;
            if(!a and  b and  c and !d and  e) res[tarA]=1, res[tarB]=1;
            if(!a and  b and  c and  d and !e) res[tarA]=1, res[tarB]=2;
            if(!a and  b and  c and  d and  e) res[tarA]=1, res[tarB]=3;
            if( a and !b and  c and !d and  e) res[tarA]=2, res[tarB]=0;
            if( a and  b and  c and  d and !e) res[tarA]=2, res[tarB]=1;
            if( a and  b and  c and !d and  e) res[tarA]=2, res[tarB]=2;
            if( a and !b and  c and  d and  e) res[tarA]=2, res[tarB]=3;
            if(!a and !b and  c and  d and  e) res[tarA]=3, res[tarB]=0;
            if( a and  b and !c and  d and  e) res[tarA]=3, res[tarB]=1;
            if( a and !b and !c and  d and  e) res[tarA]=3, res[tarB]=2;
            if( a and  b and  c and  d and  e) res[tarA]=3, res[tarB]=3;
            int curA=-1, curB=-1, curC=-1; // 两个空格和一个必要滞留位
            for(int j=i; j<=i+4; j++){
                if(nexist(p,j)) continue;
                if(curA==-1) curA=j;
                else if(curB==-1) curB=j;
                else if(curC==-1) curC=j;
            }
            tarA=curA, tarB=curB;
            left.push_back(curC);
        }
        int pos=47;
        for(int y:left){
            bool a=(!nexist(p,pos)), b=(!nexist(p,pos+1));
            if(!a and !b) res[y]=0;
            if( a and !b) res[y]=2;
            if(!a and  b) res[y]=1;
            if( a and  b) res[y]=3;
            pos+=2;
            if(pos>N) break;
        }
        return res;
    }
}