998. 起床困难综合症

题目链接

998. 起床困难综合症

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

解题思路

贪心,位运算

可以发现,位运算每一位之间相互独立,互不影响,因此,可以从高位开始考虑,有两种选择: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;
}
posted @ 2022-04-03 17:17  zyy2001  阅读(23)  评论(0编辑  收藏  举报