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

  1. //哎,自己太菜了,看了隔壁学姐的博客,然后发现,我曹。。。。
  2. //其实这一道题可以用贪心来解释,要从0~m的范围内找一个满足条件的数,不如先从一个数的二进制中想办法,
  3. //二进制中要么是0要么是1,所以从最低位开始往最高位慢慢找.
  4. //每次与操作中要求的操作进行运算,如果一个数的某一位是0经过运算之后为1,那么肯定要把这个位置取0,
  5. //相反则取1。
  6. #include<queue>
  7. #include<stack>
  8. #include<vector>
  9. #include<math.h>
  10. #include<cstdio>
  11. #include<sstream>
  12. #include<numeric>//STL数值算法头文件
  13. #include<stdlib.h>
  14. #include <ctype.h>
  15. #include<string.h>
  16. #include<iostream>
  17. #include<algorithm>
  18. #include<functional>//模板类头文件
  19. using namespace std;
  20. const int maxn=110000;
  21. typedef long long ll;
  22. const int INF=0x3f3f3f3f;
  23. struct node
  24. {
  25. char str[15];
  26. int x;
  27. } c[maxn];
  28. int a[32];
  29. void init()
  30. {
  31. a[0]=1;
  32. for(int i=1; i<32; i++)
  33. a[i]=a[i-1]*2;
  34. }
  35. int main()
  36. {
  37. int n,m;
  38. init();
  39. while(~scanf("%d%d",&n,&m))
  40. {
  41. int maxx=0;
  42. for(int i=0; i<n; i++)
  43. scanf("%s%d",c[i].str,&c[i].x);
  44. for(int i=0; a[i]<=m; i++)
  45. {
  46. int xx=a[i],yy=a[i]-1,zz=a[i]+1;
  47. for(int j=0; j<n; j++)
  48. {
  49. //cout<<c[j].str<<" "<<c[j].x<<endl;
  50. if(c[j].str[0]=='A')
  51. {
  52. xx=xx&c[j].x;
  53. yy=yy&c[j].x;
  54. zz=zz&c[j].x;
  55. }
  56. else if(c[j].str[0]=='O')
  57. {
  58. xx=xx|c[j].x;
  59. yy=yy|c[j].x;
  60. zz=zz|c[j].x;
  61. }
  62. else if(c[j].str[0]=='X')
  63. {
  64. xx=xx^c[j].x;
  65. yy=yy^c[j].x;
  66. zz=zz^c[j].x;
  67. }
  68. }
  69. maxx=max(maxx,max(xx,max(yy,zz)));
  70. }
  71. printf("%d\n",maxx);
  72. }
  73. return 0;
  74. }
  75. #include<queue>
  76. #include<stack>
  77. #include<vector>
  78. #include<math.h>
  79. #include<cstdio>
  80. #include<sstream>
  81. #include<numeric>//STL数值算法头文件
  82. #include<stdlib.h>
  83. #include <ctype.h>
  84. #include<string.h>
  85. #include<iostream>
  86. #include<algorithm>
  87. #include<functional>//模板类头文件
  88. using namespace std;
  89. const int maxn=110000;
  90. typedef long long ll;
  91. const int INF=0x3f3f3f3f;
  92. struct node
  93. {
  94. char c;
  95. int num;
  96. } a[100010];
  97. int val_1[110],bin[110],val_0[110],max_val[110];
  98. int solve(int n,int m,int max_num)
  99. {
  100. int i,j,sizes=0,ans=0;
  101. for(i=0; (1<<i)<=max_num; i++)
  102. {
  103. val_1[i]=1<<i;
  104. val_0[i]=0;
  105. for(j=1; j<=m; j++)
  106. {
  107. if(a[j].c=='A') //AND
  108. val_1[i]&=(a[j].num&(1<<i)),val_0[i]&=(a[j].num&(1<<i));
  109. else if(a[j].c=='O') //OR
  110. val_1[i]|=(a[j].num&(1<<i)),val_0[i]|=(a[j].num&(1<<i));
  111. else if(a[j].c=='X') //XOR
  112. val_1[i]^=(a[j].num&(1<<i)),val_0[i]^=(a[j].num&(1<<i));
  113. }
  114. max_val[i]=max(val_1[i],val_0[i]);
  115. }
  116. int max_sizes=0;
  117. while(max_num)
  118. max_sizes++,max_num>>=1;
  119. while(n)
  120. bin[++sizes]=n&1,n>>=1;
  121. for(i=1; i<=max_sizes; i++)
  122. {
  123. if(bin[i]==0) ans+=val_0[i-1];
  124. else
  125. {
  126. int t_ans=val_0[i-1];
  127. for(j=1; j<i; j++) t_ans+=max_val[j-1];
  128. ans=max(ans+val_1[i-1],t_ans);
  129. }
  130. }
  131. return ans;
  132. }
  133. int main()
  134. {
  135. int n,m;
  136. char s[10];
  137. while(scanf("%d%d",&m,&n)!=EOF)
  138. {
  139. int max_num=0;
  140. for(int i=1; i<=m; i++)
  141. scanf("%s%d",s,&a[i].num),a[i].c=s[0],max_num=max(max_num,a[i].num);
  142. printf("%d\n",solve(n,m,max_num));
  143. }
  144. return 0;
  145. }

哈尔滨理工大学第七届程序设计竞赛(G.Great Atm)的更多相关文章

  1. 哈尔滨理工大学第七届程序设计竞赛初赛(BFS多队列顺序)

    哈尔滨理工大学第七届程序设计竞赛初赛https://www.nowcoder.com/acm/contest/28#question D题wa了半天....(真真正正的半天) 其实D题本来就是一个简单 ...

  2. 哈尔滨理工大学第七届程序设计竞赛初赛(高年级组)I - B-旅行

    题目描述 小z放假了,准备到RRR城市旅行,其中这个城市有N个旅游景点.小z时间有限,只能在三个旅行景点进行游玩.小明租了辆车,司机很善良,说咱不计路程,只要你一次性缴费足够,我就带你走遍RRR城. ...

  3. 哈尔滨理工大学第七届程序设计竞赛决赛(网络赛-高年级组)I - 没有名字

    题目描述 tabris实在是太菜了,没打败恶龙,在绿岛也只捡到一块生铁回去了,为了不在继续拉低acimo星球的平均水平逃离地球,来到了Sabi星球. 在这里tabris发现了一种神奇的生物,这种生物不 ...

  4. 2018年长沙理工大学第十三届程序设计竞赛 G 逃离迷宫 【BFS】

    链接:https://www.nowcoder.com/acm/contest/96/G 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  5. 江西财经大学第一届程序设计竞赛 G题 小Q的口袋校园

    链接:https://www.nowcoder.com/acm/contest/115/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  6. 江西财经大学第一届程序设计竞赛 G

    链接:https://www.nowcoder.com/acm/contest/115/G来源:牛客网 题目描述 周末,小Q喜欢在PU口袋校园上参加各种活动刷绩点,体验丰富多彩的大学生活. 但是每个活 ...

  7. 陕西师范大学第七届程序设计竞赛网络同步赛 I 排队排队排队【数组任一位可以移动到队头,最少移动几次增序/数组指针操作】

    链接:https://www.nowcoder.com/acm/contest/121/I来源:牛客网 题目描述 ACM竞赛队内要开运动会啦!!!! 竞赛队内的一群阳光乐观积极的队员们迅速的在操场上站 ...

  8. 2018年长沙理工大学第十三届程序设计竞赛 E 小木乃伊到我家 【最短路】

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 AA的欧尼酱qwb是个考古学家,有一天qwb发 ...

  9. 2018年长沙理工大学第十三届程序设计竞赛 I 连续区间的最大公约数

    连续区间的最大公约数 思路:参照BZOJ 4488: [Jsoi2015]最大公约数脑补出的一个\(map\)套\(vector\)的写法,写起来比线段树短,运行时间比线段树快. 代码: #pragm ...

随机推荐

  1. 求逆元的两种方法+求逆元的O(n)递推算法

    到国庆假期都是复习阶段..所以把一些东西整理重温一下. gcd(a,p)=1,ax≡1(%p),则x为a的逆元.注意前提:gcd(a,p)=1; 方法一:拓展欧几里得 gcd(a,p)=1,ax≡1( ...

  2. MyBatis 系列五 之 延迟加载、一级缓存、二级缓存设置

    MyBatis的延迟加载.一级缓存.二级缓存设置 首先我们必须分清延迟加载的适用对象 延迟加载 MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟加载规则推迟对关联对象的se ...

  3. 完全背包问题入门 (dp)

    问题描述: 有n种重量和价值分别为Wi,Vi的物品,从这些中挑选出总重量不超过W的物品,求出挑选物品的价值总和的最大值,每种物品可以挑选任意多件. 分析: 令dp[i+1][j]表示从前i件物品中挑选 ...

  4. windows下 nginx安装 使用

    介绍 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器. 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络 ...

  5. spring cloud config 详解

    Spring Cloud 为开发人员提供了一系列的工具来快速构建分布式系统的通用模型 .例如:配置管理.服务发现.断路由.智能路由.微代理.控制总线.一次性Token.全局锁.决策竞选.分布式sess ...

  6. spring boot 注解说明

    Starters 可以创建自己的Starter,但名字格式不能是 spring-boot-starter-*,而是 *-spring-boot-starter.类似Maven插件的规则.   自动配置 ...

  7. ifa_local 和 ifa_address

    ifa_local 和 ifa_address区别联系: 1. 在配置了支持广播的接口上,与IFA_LOCAL一样,同样表示本地ip地址: 2. 对于点对点链路,IFA_ADDRESS表示的是对端的地 ...

  8. 【快速玩转Source Filmmaker】用黑科技做出自己的OC和想要的模型

    [快速玩转Source Filmmaker]用黑科技做出自己的OC和想要的模型https://tieba.baidu.com/p/4154097168

  9. gcc -rpath 指定动态库路径

    gcc -rpath 指定动态库路径 http://blog.csdn.net/v6543210/article/details/44809405

  10. vim常用命令(复习版)(转)

    原文链接:http://blog.csdn.net/love__coder/article/details/6739670 1.光标移动 上:k 下:j 左:l 『字母L小写』 右:h 上一行行首:- ...