[eJOI 2026] Elevator 题解
Blue_Fish_Junly · · 题解
鱼生第二道通信题,考察信息的传递和损耗,是好玩的。
给出一种本质相同、实现方式和细节不太相同的思路。
题目中给出的各个子任务都有不同约束,因此我们考虑逐个完成。
子任务 1
保证
0 \le v_i \le 1 ,且每个0 后必有一个1 ,要求还原60/60 个居民的秘密数字。
考虑到如果需要还原每个楼层的数字,则必须让所有有效楼层的居民都能操作电梯。
但是一巴掌按下所有按钮显然不行。我们考虑到秘密数字为
这不难做到。对于每个居民,可以执行如下操作:
- 如果秘密数字为
1 ,就按下上面楼层的按钮; - 如果秘密数字为
0 ,则上面的楼层为无效信息,直接按下两层后的按钮。
还原时,所有
因此:所有空按钮的下面一层必然是
这样就做完了。
子任务 2
保证
0 \le v_i \le 1 ,要求还原40/60 个居民的秘密数字。
也就是每
我们尝试将所有居民三个三个捆绑,每组中都出现
不难想到,信息传递的方法可以依赖于操作下组居民的电梯按钮。
那么,一次合法的信息传递需要:
- 通过接下来一组
3 个比特位,唯一确定该组内某两个居民的神秘数字; - 按下下一组中
\ge 2 个按钮,使得下一组也有至少两位可以传递信息的居民。
现在问题即转化为,如何安排下一组的比特位,使得满足上述两个条件。
不难发现,如果
问题在于,两位居民可能不知道彼此的神秘数字,因此该做法不能仅停留于理论计算。
我们考虑这个过程:第一位居民先通过按下电梯的某一位,来“通知”另外一位居民自己的神秘数字;另外一位居民通过解码和拼装两个居民的所有信息,在电梯按钮上补按按钮,完成所有过程。
而这个构造方案不难,我来随便构造一组:
- 设两位居民按照操作的先后顺序,分别为 A 和 B;为其分配的三个比特位为
w 。 - 若 A 的神秘数字为
0 ,则使w 变为100 ;否则使w 变为010 。 - B 通过拼装 A 的神秘数字和自己的神秘数字,得到
c 。- 若
c=00 ,将w 从100 变成110 ; - 若
c=01 ,将w 从100 变成101 ; - 若
c=10 ,将w 从010 变成011 ; - 若
c=11 ,将w 从010 变成111 。
- 若
特殊地,为了让每组的贡献都恰为
这样就能完成子任务 2 了。
子任务 3
保证
0 \le v_i \le 2 ,要求还原30/60 个居民的秘密数字。
也就是每
我们延续子任务 2 的想法,将整个电梯楼层剖成
考虑每组只传递一个信息,该信息有
为了让下一组能出人,我们将这些可能性一一映射到按钮状态的
然后你发现组内只有一个人,甚至不用信息传递,直接就做完了,比子任务 2 还简单。
子任务 4...?
保证
0 \le v_i \le 3 ,要求还原25/60 个居民的秘密数字。
也就是每
我们继续延续子任务 2 的想法,将整个电梯楼层剖成
考虑每组只传递五条信息,这些信息的可能性数量一共有
为了让下一组能出至少
不难算出结果为
并没有。在上文中我们提到,居民们可能不知道彼此的神秘数字,因此该做法不能仅停留于理论计算。
问题在于,每个居民只能在已经按下的按钮的基础上继续添加按钮,但绝对不能抹除按钮。
更致命的是,一组的居民只能通过之前的同组居民所按按钮判断它们的秘密数字。因此前面的信息传输也必须到位。
我们举一个例子。根据既往的构造方法,为了让下一组的一些比特和这一组的秘密数字状态之间产生尽量多的双射,第一个居民的最优安排是根据自己的神秘数字在前
因此,问题在于能否找到与信息量恰好匹配的紧凑在线编码。
十二个元素分组的构造相对困难,并且构造出来分支太多也比较吃力,我们于是弃用这个做法。
子任务 4
我们尝试找到一种相对紧凑的构建方法。
五个居民太难传递,一个居民的状态数正好占满两个比特位且可能都为
我们考虑两个居民。
注意到:两个居民的秘密数字的可能状态个数是
我们仍将居民分组,每组
则这些居民能够创造的可还原个数就是
这些按钮可以在剩余的比特位上面传递自己的信息,每两位构成的二进制数可以和神秘数字双射。
考虑按钮
现在只需要让
这个时候取
下面给出一组构造:
- 设两位居民按照操作的先后顺序,分别为 A 和 B;为其分配的五个比特位为
w 。 - A 通过自己的神秘数字,构建初始比特:
- 若 A 的神秘数字为
0 ,则使w 变为10000 ; - 若 A 的神秘数字为
1 ,则使w 变为01000 ; - 若 A 的神秘数字为
2 ,则使w 变为00100 ; - 若 A 的神秘数字为
3 ,则使w 变为00010 。
- 若 A 的神秘数字为
- B 通过拼装 A 的神秘数字和自己的神秘数字,得到最终数字,如下表所示:
| A 的数 \ B 的数 | ||||
|---|---|---|---|---|
这个限制是真的一点都不放宽啊,做这题真的有点刀尖起舞的感觉。
上述所有思路在精细实现后,可以解决此题。
最后给出算法的一张图解:
代码给出,部分实现不再花篇幅讲解,如遇细节问题可私信指正。
#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;
}
}