Codeforces Round #402 (Div2)E题
来源:互联网 发布:千里眼淘宝插件 编辑:程序博客网 时间:2024/05/24 06:42
E. Bitwise Formula
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.
Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter’s score equals to the sum of all variable values.
Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.
Input
The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).
The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign “:=”, space, followed by one of:
1.Binary number of exactly m bits.
2.The first operand, space, bitwise operation (“AND”, “OR” or “XOR”), space, the second operand. Each operand is either the name of variable defined before or symbol ‘?’, indicating the number chosen by Peter.
Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.
Output
In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.
Examples
Input
3 3
a := 101
b := 011
c := ? XOR b
Output
011
100
Input
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
Output
0
0
Note
In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15.
For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn’t depend on the number chosen by Peter, so the minimum Peter can choose is 0.
题意:给n个等式,其中变量的长度为m位(2进制),变量中会有?,问?取多少,所有变量的和最大,求出最大时最大的?和最小的?。
解法:
想到对每一位进行枚举,这题就是一个模拟题。因为转为2进制后,每一位都是独立的。那么那一位只有0和1两种选择,很容易算出最大最小值。
代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<string>using namespace std;const int N=5005,M=1005;int n,m;map<string,int>mp;char val[N][12];struct Node{ int op; int op1; int op2; int str[M];}node[N];int tmpval[N];char Min[M],Max[M];int bit(int op1,int op2,int op){ if(op==1) return op1&op2; if(op==2) return op1|op2; return op1^op2;}void cal(int pos){ int ans0=0; for(int i=1;i<=n;i++){ if(node[i].op==0) ans0+=node[i].str[pos],tmpval[i]=node[i].str[pos]; else{ int op1=node[i].op1==0?0:tmpval[node[i].op1]; int op2=node[i].op2==0?0:tmpval[node[i].op2]; ans0+=bit(op1,op2,node[i].op); tmpval[i]=bit(op1,op2,node[i].op); } } int ans1=0; for(int i=1;i<=n;i++){ if(node[i].op==0) ans1+=node[i].str[pos]; else{ int op1=node[i].op1==0?1:tmpval[node[i].op1]; int op2=node[i].op2==0?1:tmpval[node[i].op2]; ans1+=bit(op1,op2,node[i].op); tmpval[i]=bit(op1,op2,node[i].op); } } if(ans1>ans0){ Max[pos]=1+'0'; Min[pos]=0+'0'; }else if(ans1==ans0){ Max[pos]=0+'0'; Min[pos]=0+'0'; }else{ Max[pos]=0+'0'; Min[pos]=1+'0'; }}int main(){ scanf("%d%d",&n,&m); getchar(); char tmp[3*M]; for(int i=1;i<=n;i++){ scanf(" %s",val[i]); mp[val[i]]=i; scanf(" %s",tmp); scanf(" %s",tmp); if(tmp[0]=='0'||tmp[0]=='1'){ node[i].op=0; for(int j=0;j<m;j++) node[i].str[j]=tmp[j]-'0'; continue; } if(tmp[0]=='?') node[i].op1=0; else node[i].op1=mp[tmp]; scanf("%s",tmp); if(tmp[0]=='A'){ node[i].op=1; }else if(tmp[0]=='O') node[i].op=2; else node[i].op=3; scanf("%s",tmp); if(tmp[0]=='?') node[i].op2=0; else node[i].op2=mp[tmp]; } for(int i=0;i<m;i++){ cal(i); } Max[m]=Min[m]='\0'; printf("%s\n",Min); printf("%s\n",Max); return 0;}
- Codeforces Round #402 (Div2)E题
- Codeforces Round#99 E(Div2)
- codeforces div2 round#230 E
- codeforces Round 377 Div2 E
- codeforces Round #237(div2) E解题报告
- codeforces Round #241(div2) E解题报告
- codeforces Round #264(div2) E解题报告
- codeforces Round #263(div2) E解题报告
- codeforces Round #274(div2) E解题报告
- codeforces Round #261(div2) E解题报告
- codeforces Round #260(div2) E解题报告
- codeforces Round #259(div2) E解题报告
- Codeforces Round #424 Div2 E. Cards Sorting
- Codeforces Round #402 (Div2)C题
- Codeforces Round #402 (Div2)D题
- codeforces Codeforces Round #407 Div2题解 B,C,D,E
- CodeForces Round #112 Div2 165 E. Compatible Numbers
- Codeforces Round #175 DIV2 E Positions in Permutations
- 从一个游戏开始看设计(二)
- 新手上路
- 利用RODBC包读取并展示xls文件内容
- Tensorflow学习笔记
- 【LeetCode】338. Counting Bits
- Codeforces Round #402 (Div2)E题
- 正在读取软件包列表... 有错误!
- 【java】通过抽象类定义车类的模板,然后通过抽象的车类派生拖拉机、卡车、小轿车。
- 设计模式(13)-代理模式
- 一日一搏:HQL 语句总结
- bzoj3892
- 解决keil C编译出现ERROR L107: ADDRESS SPACE OVERFLOW
- Android Broadcast 简易版手机勒索软件
- php之 session