哈尔滨理工大学第七届程序设计竞赛(G.Great Atm)
Description
An old story said the evil dragon wasn’t evil at all, only bewitched, and now that the riddles were solved it was proving to be as kind as its new master.
A powerful warrior Atm is going to solve the riddles. First, he should beat the evil wizard. The road from Atm’s castle to wizard’s lab is filled with magic traps. The magic trap will affect Atm’s combat effectiveness.
Atm’s combat effectiveness can be considered as an integer. Effect of magic trap can be considered as mathematical operation. The three kinds of magic traps correspond to three kind of bit operation. (AND, OR and XOR)
Atm can adjust his equipment to change his initial combat effectiveness from 0 to m (include 0 and m). He wants when he arrives the wizard’s lab, his combat effectiveness can be maximum.
Input
There are multiple test cases.
For each test cases:
The first line contains two integers n(1<=n<=10^5) and m(1<=m<=10^9), indicating the number of magic traps and the maximum of initial combat effectiveness.
Each of the next n lines contains a string and an integer, indicating the bit operation. The string will be “AND”, “OR” or “XOR” correspond to AND operation (&), OR operation (|) or XOR operation (^). The integer t(1<=t<=10^9) is second operand in the operation.
Output
For each test cases, a line contains an integer, indicating the maximum combat effectiveness when he arrives the wizard’s lab.
Sample Input
3 10
AND 5
OR 6
XOR 7
Sample Output
1
//哎,自己太菜了,看了隔壁学姐的博客,然后发现,我曹。。。。
//其实这一道题可以用贪心来解释,要从0~m的范围内找一个满足条件的数,不如先从一个数的二进制中想办法,
//二进制中要么是0要么是1,所以从最低位开始往最高位慢慢找.
//每次与操作中要求的操作进行运算,如果一个数的某一位是0经过运算之后为1,那么肯定要把这个位置取0,
//相反则取1。
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include <ctype.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
const int maxn=110000;
typedef long long ll;
const int INF=0x3f3f3f3f;
struct node
{
char str[15];
int x;
} c[maxn];
int a[32];
void init()
{
a[0]=1;
for(int i=1; i<32; i++)
a[i]=a[i-1]*2;
}
int main()
{
int n,m;
init();
while(~scanf("%d%d",&n,&m))
{
int maxx=0;
for(int i=0; i<n; i++)
scanf("%s%d",c[i].str,&c[i].x);
for(int i=0; a[i]<=m; i++)
{
int xx=a[i],yy=a[i]-1,zz=a[i]+1;
for(int j=0; j<n; j++)
{
//cout<<c[j].str<<" "<<c[j].x<<endl;
if(c[j].str[0]=='A')
{
xx=xx&c[j].x;
yy=yy&c[j].x;
zz=zz&c[j].x;
}
else if(c[j].str[0]=='O')
{
xx=xx|c[j].x;
yy=yy|c[j].x;
zz=zz|c[j].x;
}
else if(c[j].str[0]=='X')
{
xx=xx^c[j].x;
yy=yy^c[j].x;
zz=zz^c[j].x;
}
}
maxx=max(maxx,max(xx,max(yy,zz)));
}
printf("%d\n",maxx);
}
return 0;
}
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include <ctype.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
const int maxn=110000;
typedef long long ll;
const int INF=0x3f3f3f3f;
struct node
{
char c;
int num;
} a[100010];
int val_1[110],bin[110],val_0[110],max_val[110];
int solve(int n,int m,int max_num)
{
int i,j,sizes=0,ans=0;
for(i=0; (1<<i)<=max_num; i++)
{
val_1[i]=1<<i;
val_0[i]=0;
for(j=1; j<=m; j++)
{
if(a[j].c=='A') //AND
val_1[i]&=(a[j].num&(1<<i)),val_0[i]&=(a[j].num&(1<<i));
else if(a[j].c=='O') //OR
val_1[i]|=(a[j].num&(1<<i)),val_0[i]|=(a[j].num&(1<<i));
else if(a[j].c=='X') //XOR
val_1[i]^=(a[j].num&(1<<i)),val_0[i]^=(a[j].num&(1<<i));
}
max_val[i]=max(val_1[i],val_0[i]);
}
int max_sizes=0;
while(max_num)
max_sizes++,max_num>>=1;
while(n)
bin[++sizes]=n&1,n>>=1;
for(i=1; i<=max_sizes; i++)
{
if(bin[i]==0) ans+=val_0[i-1];
else
{
int t_ans=val_0[i-1];
for(j=1; j<i; j++) t_ans+=max_val[j-1];
ans=max(ans+val_1[i-1],t_ans);
}
}
return ans;
}
int main()
{
int n,m;
char s[10];
while(scanf("%d%d",&m,&n)!=EOF)
{
int max_num=0;
for(int i=1; i<=m; i++)
scanf("%s%d",s,&a[i].num),a[i].c=s[0],max_num=max(max_num,a[i].num);
printf("%d\n",solve(n,m,max_num));
}
return 0;
}
哈尔滨理工大学第七届程序设计竞赛(G.Great Atm)的更多相关文章
- 哈尔滨理工大学第七届程序设计竞赛初赛(BFS多队列顺序)
哈尔滨理工大学第七届程序设计竞赛初赛https://www.nowcoder.com/acm/contest/28#question D题wa了半天....(真真正正的半天) 其实D题本来就是一个简单 ...
- 哈尔滨理工大学第七届程序设计竞赛初赛(高年级组)I - B-旅行
题目描述 小z放假了,准备到RRR城市旅行,其中这个城市有N个旅游景点.小z时间有限,只能在三个旅行景点进行游玩.小明租了辆车,司机很善良,说咱不计路程,只要你一次性缴费足够,我就带你走遍RRR城. ...
- 哈尔滨理工大学第七届程序设计竞赛决赛(网络赛-高年级组)I - 没有名字
题目描述 tabris实在是太菜了,没打败恶龙,在绿岛也只捡到一块生铁回去了,为了不在继续拉低acimo星球的平均水平逃离地球,来到了Sabi星球. 在这里tabris发现了一种神奇的生物,这种生物不 ...
- 2018年长沙理工大学第十三届程序设计竞赛 G 逃离迷宫 【BFS】
链接:https://www.nowcoder.com/acm/contest/96/G 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 江西财经大学第一届程序设计竞赛 G题 小Q的口袋校园
链接:https://www.nowcoder.com/acm/contest/115/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 江西财经大学第一届程序设计竞赛 G
链接:https://www.nowcoder.com/acm/contest/115/G来源:牛客网 题目描述 周末,小Q喜欢在PU口袋校园上参加各种活动刷绩点,体验丰富多彩的大学生活. 但是每个活 ...
- 陕西师范大学第七届程序设计竞赛网络同步赛 I 排队排队排队【数组任一位可以移动到队头,最少移动几次增序/数组指针操作】
链接:https://www.nowcoder.com/acm/contest/121/I来源:牛客网 题目描述 ACM竞赛队内要开运动会啦!!!! 竞赛队内的一群阳光乐观积极的队员们迅速的在操场上站 ...
- 2018年长沙理工大学第十三届程序设计竞赛 E 小木乃伊到我家 【最短路】
时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 AA的欧尼酱qwb是个考古学家,有一天qwb发 ...
- 2018年长沙理工大学第十三届程序设计竞赛 I 连续区间的最大公约数
连续区间的最大公约数 思路:参照BZOJ 4488: [Jsoi2015]最大公约数脑补出的一个\(map\)套\(vector\)的写法,写起来比线段树短,运行时间比线段树快. 代码: #pragm ...
随机推荐
- 创建Fragment和传递数值
下面在扩展一下创建Fragment和传递数值 如果我们不需要传递数值,那就直接可以在宿主activity中,跟平常一样创建fragment,但是如果我们需要传递数据的话,可以使用newInstance ...
- 常见网络命令之Ping命令
前言:计算机网络老师要求我们自己总结一下常见的网络命,然后上课可以上去讲一下这些命令使用,像我这么听话的好学生,肯定是照老师要求,认真的总结了一下,总结的过程中,我发现网上已经有的资源讲的都不是很详细 ...
- c语言中网络字节序和主机字节序的转换
函数说明 相关函数:htonl, htons, ntohl 头文件:#include <netinet/in.h> 定义函数:unsigned short int ntohs(unsi ...
- python之提速千倍爆破一句话
看了一下冰河大佬写的文章特别有感:https://bbs.ichunqiu.com/thread-16952-1-1.html 简单描述一下: 利用传统的单数据提交模式. 比如下面这个一句话木马: & ...
- 阿里CDN核心技术解密
1. 阿里CDN组件分层 其中应用层主要用到的技术有负载均衡和缓存, 负载均衡包括全局负载均衡和本地负载均衡; 缓存通过HTTP缓存服务器Swift做HTTP缓存. 全局负载均衡以DNS服务器Phar ...
- js判断文件格式及大小
//判断照片大小 function getPhotoSize(obj){ photoExt=obj.value.substr(obj.value.lastIndexOf(".&q ...
- 线程同步工具 Semaphore类使用案例
参考博文 : 线程同步工具(一) 线程同步工具(二)控制并发访问多个资源 并发工具类(三)控制并发线程数的Semaphore 使用Semaphore模拟互斥锁 当一个线程想要访问某个共享资源,首先,它 ...
- Disruptor Ringbuffer
系列译文: http://ifeve.com/disruptor/ 当有多个消费者时,(按Disruptor的设计)每个消费者各自控制自己的指针,依次读取每个Slot(也就是每个消费者都会读取到所有的 ...
- selenium+python自动化78-autoit参数化与批量上传【转载】
转至博客:上海-悠悠 前言前一篇autoit实现文件上传打包成.exe可执行文件后,每次只能传固定的那个图片,我们实际测试时候希望传不同的图片.这样每次调用的时候,在命令行里面加一个文件路径的参数就行 ...
- 在cmd 中输入了错误mysql命令后,如何退出?
例如: mysql> select * from tb_name '> '> '> '> 由于输错 ...