当前位置:网站首页>起床困难综合症(按位贪心)
起床困难综合症(按位贪心)
2022-07-01 23:49:00 【why151】
题目描述:
主要思路:
本题主要考察贪心与位运算。
a1=0:
在穿越完保护罩之后,第i位=1,那么ans可以直接+1<<i,不需要满足其他条件。
a2=0:
在穿越完保护罩之后,第i位=1,需要满足(1<<i)<=m,因为需要一开始的x的第i位=1。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int a1=0,a2=-1;
for(int i=1;i<=n;i++)
{
string op;
int x;
cin>>op>>x;
if(op=="AND") a1&=x,a2&=x;
if(op=="OR") a1|=x,a2|=x;
if(op=="XOR") a1^=x,a2^=x;
}
int ans=0;
for(int i=0;i<30;i++)
{
if(a1>>i&1) ans|=1<<i;
else if(a2>>i&1)
{
if((1<<i)<=m)
{
ans|=1<<i;
m-=1<<i;
}
}
}
cout<<ans<<endl;
return 0;
}
边栏推荐
猜你喜欢
Redis AOF日志
What professional classification does the application of Internet of things technology belong to
【QT】Qt 使用MSVC2017找不到编译器的解决办法
The essence of software architecture
[QT] test whether QT can connect to the database
Pytorch learning record
使用VB.net将PNG图片转成icon类型图标文件
Redis RDB快照
【CMake】Qt creator 里面的 cmake 配置
Kubernetes resource object introduction and common commands (III)
随机推荐
ADO.NET之SqlDataAdpter对象
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
Li Kou today's question -241 Design priorities for operational expressions
Regular expression collection
第六章 数据流建模
Door level modeling - after class exercises
The essence of software architecture
北京炒股开户选择手机办理安全吗?
ARP报文头部格式和请求流程
【QT】Qt 使用MSVC2017找不到编译器的解决办法
SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
Concepts of dictionary, hash table and array
What category does the Internet of things application technology major belong to
De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
S32Kxxx bootloader之UDS bootloader
.env.xxx 文件,加了常量,却undefined
Material design component - use bottomsheet to show extended content (I)
ADO. Net SqlCommand object
Openwrt enable kV roaming
MySQL: the difference between insert ignore, insert and replace