当前位置:网站首页>起床困难综合症(按位贪心)
起床困难综合症(按位贪心)
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;
}
边栏推荐
- Applet form verification encapsulation
- Use pair to do unordered_ Key value of map
- Practical application and extension of plain framework
- 2021 robocom world robot developer competition - preliminary competition of higher vocational group
- 比较通俗易懂的PID理解
- 深度学习 | 三个概念:Epoch, Batch, Iteration
- Is it safe to buy funds on Great Wall Securities?
- 【CMake】Qt creator 里面的 cmake 配置
- excel如何打开100万行以上的csv文件
- cookie、session、tooken
猜你喜欢
【QT】对于Qt MSVC 2017无法编译的问题解决
字典、哈希表、数组的概念
How to solve the image pop-up problem when pycharm calls Matplotlib to draw
2022年最佳智能家居开源系统:Alexa、Home Assistant、HomeKit生态系统介绍
Is there a piece of code that makes you convinced by human wisdom
E-commerce RPA robot helps brand e-commerce to achieve high traffic
2021 robocom world robot developer competition - semi finals of higher vocational group
Linux foundation - centos7 offline installation of MySQL
Zero foundation tutorial of Internet of things development
比较通俗易懂的PID理解
随机推荐
【CMake】Qt creator 里面的 cmake 配置
URL introduction
PostgreSQL notes (10) dynamically execute syntax parsing process
Distance measurement - Hamming distance
哈工大《信息内容安全》课程知识要点和难点
ADO. Net SqlDataAdapter object
algolia 搜索需求,做的快自闭了...
小程序表单校验封装
Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
软件架构的本质
[untitled]
E-commerce RPA robot helps brand e-commerce to achieve high traffic
S32Kxxx bootloader之UDS bootloader
Depth first search and breadth first search of graph traversal
【ES实战】ES上的安全性运行方式
SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
How excel opens CSV files with more than one million lines
PostgreSQL source code (58) tuple splicing heap_ form_ Tuple analysis
2021 robocom world robot developer competition - preliminary competition of higher vocational group
【必会】BM41 输出二叉树的右视图【中等+】