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 | ||||||
Source | ||||||
"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 |
题目大意:
给你N个操作,一开始可以选择【0~m】中的一个数作为起点,然后进行N个操作之后,得到结果,现在问你最大的结果。
思路:
听说数据很水?
1、首先,对于一个结果,我们如果转化成二进制,那么对应第i位的结果,只受到初始的那个数第i位是什么,(0或者1);
那么我们可以预处理出来,对应每一位,初始的时候是0和1的结果。
2、那么接下来进行贪心,如果一个位子上,原来初始的时候是0就能得到1,那么肯定我们这一位初始的数直接设定为0即可。
那么如果原来初始的时候是0并不能得到1,那么再看看原来初始的时候是1是否能够得到1.如果可以,那么我们可以考虑这个位子是否取1.
那么如何考虑取1的方案呢?
很明显,我们从高位开始贪心即可。
那么我们过程讨论从高位开始,如果这一位:
①这一位初始是0结果是1.那么取0.否则转②
②这一位初始是1结果是1.那么取1.否则取0.因为此时取0取1都一样,那么我们肯定希望此时数小更好,因为之后还有可能取1的地方很多,留给那些位子取1是最优的。
③过程维护好数字不要超过上限m即可。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll long long int
ll n,m;
char a[105000][10];
ll non[105000];
ll num[105000];
ll poww[105000];
ll use[10500];
void init()
{
for(int i=0; i<=35; i++)
{
if(i==0)poww[i]=1;
else
poww[i]=poww[i-1]*2;
}
}
int main()
{
init();
while(~scanf("%lld%lld",&n,&m))
{
memset(non,0,sizeof(non));
memset(use,0,sizeof(use));
for(int i=0; i<n; i++)
{
scanf("%s%d",a[i],&num[i]);
}
for(int i=35; i>=0; i--)
{
if(poww[i]<=m)
{
ll ceshi2=0;
ll ceshi=poww[i];
for(int j=0; j<n; j++)
{
if(a[j][0]=='A')
{
ceshi=(ceshi&num[j]);
ceshi2=(ceshi2&num[j]);
}
if(a[j][0]=='O')
{
ceshi=(ceshi|num[j]);
ceshi2=(ceshi2|num[j]);
}
if(a[j][0]=='X')
{
ceshi=(ceshi^num[j]);
ceshi2=(ceshi2^num[j]);
}
}
if((ceshi2&poww[i])>0)non[i]=1;
if((ceshi&poww[i])>0)use[i]=1;
}
}
ll yu=m;
ll outputnum=0;
for(int i=35; i>=0; i--)
{
if(non[i]==1)continue;
if(use[i]==1)
{
if(yu>=poww[i])
{
yu-=poww[i];
outputnum+=poww[i];
}
}
}
for(int j=0; j<n; j++)
{
if(a[j][0]=='A')
{
outputnum=(outputnum&num[j]);
}
if(a[j][0]=='O')
{
outputnum=(outputnum|num[j]);
}
if(a[j][0]=='X')
{
outputnum=(outputnum^num[j]);
}
}
printf("%lld\n",outputnum);
}
}