当前位置: 首页 > news >正文

[IOI 1998 / USACO2.2] 派对灯 Party Lamps 题解 + bitset浅谈

现在有这些按钮:

  • 按钮 \(1\):当按下此按钮,将改变所有的灯:本来亮着的灯就熄灭,本来是关着的灯被点亮;
  • 按钮 \(2\):当按下此按钮,将改变所有奇数号的灯;
  • 按钮 \(3\):当按下此按钮,将改变所有偶数号的灯;
  • 按钮 \(4\):当按下此按钮,将改变所有序号是 \(3k+1 \ (k \in [0,+\infty) \cap \mathbb Z)\) 的灯。例如:\(1,4,7,10 \dots\)

同时告诉你 \(n\) 的大小,以及按按钮的次数 \(c\) 和某些点的状态,求最后有几种情况。

题解

注意到只有 \(0,1\) 两种状态,考虑使用 \(bitset\)

bitset

可以看作是一个二进制数,且支持以下几种操作:
reset()清零一个 \(bitset\),及将其中每一位设置为 \(0\)
set()\(bitset\) 的每一位设置为 \(1\)
test(i)返回 \(bitset\) 内第 \(i\) 位的值(从零开始,其实像数组一样直接用下标访问也可以)
any()返回 \(bitset\) 内是否有元素的值为 \(1\)
none()any()相对,如果元素全部为 \(0\) 则返回 \(1\),否则返回 \(0\)
count()计算 \(bitset\) 内元素值为 \(1\) 的元素个数(求 \(0\) 的个数直接用长度减去它就好了)。
flip()\(bitset\) 内的所有元素翻转,相当于按位取反(也就是 \(\sim\))。
好的以上就是 \(bitset\) 的所有操作。
\(bitset\) 的每一位只占一个 \(bit\),空间比桶更优。

实现所有按钮

现在让我们回到题目,根据题意以下就是所有按钮的实现:

点击查看代码
bitset<100> bottom1(bitset<100> b){b.flip();return b;
}
bitset<100> bottom2(bitset<100> b){for(int i=1;i<=n;i+=2){b.flip(i-1);}return b;
}
bitset<100> bottom3(bitset<100> b){for(int i=2;i<=n;i+=2){b.flip(i-1);}return b;
}
bitset<100> bottom4(bitset<100> b){for(int i=0;3*i+1<=n;i++){b.flip(3*i);}return b;
}

为了判定最后的结果是否符合要求,我们需要下面这个函数,而答案要从小到大排序,我们还需要一个大小比较函数(\(bitset\) 不能直接比较):

点击查看代码
bool cmp(bitset<100> A , bitset<100> B){for(int i=0;i<100;i++){if(A[i]<B[i])return true;if(A[i]>B[i])return false;}return false;
}
bool check(bitset<100> tmp){for(int i=0;i<n;i++){if(tmp[i]==0&&light[i+1]==1)return false;if(tmp[i]==1&&light[i+1]==-1)return false;}return true;
}

每个按钮的性质

注意到任意一个按钮按两次就相当于没按,白白浪费了两次按按钮的机会(看作总共 \(c\) 次)。
手玩一下(假设总共 \(10\) 个灯,操作序列:\(bottom1,bottom2,bottom1\)):

初始状态:1111111111
bottom1:0000000000
bottom2:1010101010
bottom1:0101010101

而把两个 \(bottom1\) 抵消之后的操作:

初始状态:1111111111
bottom2:0101010101

结果完全一致,证明的我们的假设是对的。

解法

现在就很好解了,列出每个按钮最多一次的所有可能操作序列,然后判断 \(c\) 减去这个次数是否为 \(2\) 的倍数,如果可以,说明这个有可能是答案之一。
主函数部分:

点击查看代码
if(c%2==0){if(check(first))ans[++len] = first;}if(c>=1&&(c-1)%2==0){if(check(bottom1(first)))ans[++len] = bottom1(first);if(check(bottom2(first)))ans[++len] = bottom2(first);if(check(bottom3(first)))ans[++len] = bottom3(first);if(check(bottom4(first)))ans[++len] = bottom4(first);}if(c>=2&&(c-2)%2==0){if(check(bottom1(bottom2(first))))ans[++len] = bottom1(bottom2(first));if(check(bottom1(bottom3(first))))ans[++len] = bottom1(bottom3(first));if(check(bottom1(bottom4(first))))ans[++len] = bottom1(bottom4(first));if(check(bottom2(bottom3(first))))ans[++len] = bottom2(bottom3(first));if(check(bottom2(bottom4(first))))ans[++len] = bottom2(bottom4(first));if(check(bottom3(bottom4(first))))ans[++len] = bottom3(bottom4(first));}if(c>=3&&(c-3)%2==0){if(check(bottom1(bottom2(bottom3(first)))))ans[++len] = bottom1(bottom2(bottom3(first)));if(check(bottom1(bottom2(bottom4(first)))))ans[++len] = bottom1(bottom2(bottom4(first)));if(check(bottom1(bottom3(bottom4(first)))))ans[++len] = bottom1(bottom3(bottom4(first)));if(check(bottom2(bottom3(bottom4(first)))))ans[++len] = bottom2(bottom3(bottom4(first)));}if(c>=4&&(c-4)%2==0){if(check(bottom1(bottom2(bottom3(bottom4(first))))))ans[++len] = bottom1(bottom2(bottom3(bottom4(first))));}sort(ans+1,ans+len+1,cmp);for(int i=1;i<=len;i++){for(int j=0;j<n;j++)cout << ans[i][j];cout << "\n";}if(len==0)cout << "IMPOSSIBLE";
一道 $bitset$ 简单题。
http://www.zskr.cn/news/15120.html

相关文章:

  • 2025 --【J+S 二十连测】-- 第一套 总结
  • 【实验报告】华东理工大学随机信号处理实验报告 - 详解
  • Docker部署配置全流程(超详细——Windows和Linux) - 指南
  • AT_abc309_g [ABC309G] Ban Permutation
  • 在Mac上运行Windows 365的完整指南
  • 完整教程:华为海思正式进入Wi-Fi FEM赛道?
  • 摩刻S10 动感单车 速度传感器故障及更换!
  • 2025硫酸优质厂家权威推荐榜:高品质与强供应口碑之选
  • 2025冰乙酸供应厂家权威推荐榜:品质卓越与市场口碑双重保障
  • 工业氨水优质厂家推荐:实力制造商深度解析与选购指南
  • 2025液碱厂家权威推荐榜:实力供应商深度解析与选择指南
  • 2025硫铵厂家权威推荐榜:实力生产与优质供应口碑之选
  • 2025年硫化钠厂家权威推荐榜:优质供应商与实力制造商精选
  • 2025 年热压机厂家 TOP 企业品牌推荐排行榜,深度剖析河北热压机,廊坊热压机,霸州热压机推荐这十家公司!
  • 【Anthropic好文】AI 代理的高效上下文工程
  • 2025防撞护栏厂家 TOP 企业品牌推荐排行榜,铝合金,Q235 桥梁,Q355B 桥梁,景观桥梁,灯光桥梁,河道桥梁,公路桥梁,喷塑桥梁,道路桥梁防撞护栏公司推荐!
  • 摩尔线程之后,看燧原科技,相关公司梳理
  • Java形式化验证实战:TLA+模型检测保障分布式事务最终一致性 - 教程
  • 2025折弯机厂家TOP企业品牌推荐排行榜,数控折弯机,电液伺服折弯机,电液折弯机,小型折弯机,液压折弯机推荐这十家公司!
  • 关于践行「AI元人文」理念、迈向审慎智慧的倡议书
  • 详细介绍:在 Ubuntu 24.04 LTS 上安装 SSH 并启用服务端实现远程连接
  • 【VSCode中Java制作环境设置的三个层级之基础篇】(Windows版)
  • 2025变压器厂家 TOP 企业品牌推荐排行榜,低压变压器,单相,三相,特种,定制,非标,配电,节能,光伏,隔离变压器推荐这十家公司!
  • 我的路由器
  • 从DevEco Studio开始:华为鸿蒙应用制作完整指南
  • 完整教程:华为手机Bootloader一键解锁工具指南
  • 2025 年离合器厂家 TOP 企业品牌推荐排行榜, CB 离合器,VC 离合器,矿山离合器气胎离合器,通风式离合器,推盘离合器,断开通风离合器,KB 离合器,PTO 离合器公司推荐!
  • 观后感 —— 鸿蒙真的创新了吗?我找到了一篇论文
  • 2025 年无锡硫化机厂家 TOP 企业品牌推荐排行榜,胶带硫化机,接头硫化机,水冷硫化机,轻型硫化机,皮带输送硫化机,电热式硫化机公司推荐!
  • 2025 年环氧地坪厂家 TOP 企业品牌推荐排行榜,环氧地坪,聚氨酯超耐磨地坪,固化剂地坪,水性聚氨酯砂浆地坪,环氧地坪漆施工,密封固化剂地坪施工公司推荐!