1. 程式人生 > >哈爾濱理工大學第七屆程式設計競賽(G.Great Atm)

哈爾濱理工大學第七屆程式設計競賽(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; }