998. 起床困难综合症

题目链接

998. 起床困难综合症

一个 boss 的防御战线由 n 扇防御门组成, 其中第 i 扇防御门的属性包括一个运 算 opi 和一个参数 ti, 运算一定是 OR、XOR、AND 中的一种, 参数是非负整数。若 在末通过这扇防御门时攻击力为 x, 则通过这扇防御门后攻击力将变为 xopiti 。最终 boss 受到的伤害为玩家的初始攻击力 x0 依次经过所有 n 扇防御门后得到的攻击力。 由于水平有限, 玩家的初始攻击力只能为 [0,m] 之间的一个整数。玩家希望通过选择 合适的初始攻击力, 使他的攻击能造成最大的伤害, 求这个伤害值。
数据范围: n105,m,ti109

解题思路

贪心,位运算

可以发现,位运算每一位之间相互独立,互不影响,因此,可以从高位开始考虑,有两种选择:1. 当前位为 0,2. 当前位为 1,分类讨论其结果:如果当前位为 0 的结果为 1,则肯定选上,因为此时不增加当前数反而还使伤害增大了,如果当前位为 0 的结果为 0,且当前位为 1 的结果为 0,则此时伤害不变,当前数不变,否则如果当前位为 1 的结果为 1 且当前数不超过 m,则选上

  • 时间复杂度:O(30n)

代码

// Problem: 起床困难综合症 // Contest: AcWing // URL: https://www.acwing.com/problem/content/1000/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=1e5; pair<string,int> a[N]; int n,m; int cal(int bit,int x) { for(int i=1;i<=n;i++) { int y=(a[i].se>>bit&1); if(a[i].fi=="AND")x&=y; else if(a[i].fi=="OR")x|=y; else x^=y; } return x; } int main() { cin>>n>>m; for(int i=1;i<=n;i++) { string s; int x; cin>>s>>x; a[i]={s,x}; } int res=0,ans=0; for(int i=29;i>=0;i--) { int res1=cal(i,1),res0=cal(i,0); if(res0==1||res1==0) { if(res0==1) ans+=1<<i; } else if(res1==1) { if(res+(1<<i)<=m)res+=1<<i,ans+=1<<i; } } cout<<ans<<'\n'; return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16096496.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示