【推导】【贪心】Codeforces Round #402 (Div. 2) E. Bitwise Formula
按位考虑,每个变量最终的赋值要么是必为0,要么必为1,要么和所选定的数相同,记为2,要么和所选定的数相反,记为3,一共就这四种情况。
可以预处理出来一个真值表,然后从前往后推导出每个变量的赋值。
然后从高位到低位考虑,如果某一位,对于所有变量而言,2的数量大于等于3的数量,就把所选定的数的该位记为0,否则记为1,就能满足和最小。反之就能满足和最大。
#include<cstdio>
#include<string>
#include<iostream>
#include<map>
using namespace std;
map<string,int>ma;
int n,m;
string a[5010],x[5010],op[5010],y[5010];
string b[5010];
int main()
{
// freopen("e.in","r",stdin);
string t;
scanf("%d%d",&n,&m);
bool flag=0;
for(int i=1;i<=n;++i)
{
if(!flag)
cin>>a[i];
ma[a[i]]=i;
flag=0;
cin>>t;
cin>>x[i]>>op[i];
if(!(op[i]=="AND" || op[i]=="OR" || op[i]=="XOR"))
{
a[i+1]=op[i];
op[i]="";
flag=1;
}
else
cin>>y[i];
}
for(int i=1;i<=n;++i)
if(op[i]=="")
b[i]=x[i];
else if(op[i]=="AND")
{
if(x[i]=="?" && y[i]=="?")
{
for(int j=0;j<m;++j)
b[i]+="2";
}
else if(x[i]=="?")
{
string ts=b[ma[y[i]]];
for(int j=0;j<m;++j)
if(ts[j]=='0') b[i]+="0";
else if(ts[j]=='1') b[i]+="2";
else if(ts[j]=='2') b[i]+="2";
else b[i]+="0";
}
else if(y[i]=="?")
{
string ts=b[ma[x[i]]];
for(int j=0;j<m;++j)
if(ts[j]=='0') b[i]+="0";
else if(ts[j]=='1') b[i]+="2";
else if(ts[j]=='2') b[i]+="2";
else b[i]+="0";
}
else
{
string ts=b[ma[x[i]]],ts2=b[ma[y[i]]];
for(int j=0;j<m;++j)
if(ts[j]<='1' && ts2[j]<='1')
b[i]+=(((ts[j]-'0')&(ts2[j]-'0'))+'0');
else if(ts[j]=='0') b[i]+="0";
else if(ts[j]=='1')
{
if(ts2[j]=='2') b[i]+="2";
else b[i]+="3";
}
else if(ts[j]=='2')
{
if(ts2[j]=='0') b[i]+="0";
else if(ts2[j]=='1') b[i]+="2";
else if(ts2[j]=='2') b[i]+="2";
else b[i]+="0";
}
else
{
if(ts2[j]=='0') b[i]+="0";
else if(ts2[j]=='1') b[i]+="3";
else if(ts2[j]=='2') b[i]+="0";
else b[i]+="3";
}
}
}
else if(op[i]=="OR")
{
if(x[i]=="?" && y[i]=="?")
{
for(int j=0;j<m;++j)
b[i]+="2";
}
else if(x[i]=="?")
{
string ts=b[ma[y[i]]];
for(int j=0;j<m;++j)
if(ts[j]=='0') b[i]+="2";
else if(ts[j]=='1') b[i]+="1";
else if(ts[j]=='2') b[i]+="2";
else b[i]+="1";
}
else if(y[i]=="?")
{
string ts=b[ma[x[i]]];
for(int j=0;j<m;++j)
if(ts[j]=='0') b[i]+="2";
else if(ts[j]=='1') b[i]+="1";
else if(ts[j]=='2') b[i]+="2";
else b[i]+="1";
}
else
{
string ts=b[ma[x[i]]],ts2=b[ma[y[i]]];
for(int j=0;j<m;++j)
if(ts[j]<='1' && ts2[j]<='1')
b[i]+=(((ts[j]-'0')|(ts2[j]-'0'))+'0');
else if(ts[j]=='1') b[i]+="1";
else if(ts[j]=='0')
{
if(ts2[j]=='2') b[i]+="2";
else b[i]+="3";
}
else if(ts[j]=='2')
{
if(ts2[j]=='0') b[i]+="2";
else if(ts2[j]=='1') b[i]+="1";
else if(ts2[j]=='2') b[i]+="2";
else b[i]+="1";
}
else
{
if(ts2[j]=='0') b[i]+="3";
else if(ts2[j]=='1') b[i]+="1";
else if(ts2[j]=='2') b[i]+="1";
else b[i]+="3";
}
}
}
else
{
if(x[i]=="?" && y[i]=="?")
{
for(int j=0;j<m;++j)
b[i]+="0";
}
else if(x[i]=="?")
{
string ts=b[ma[y[i]]];
for(int j=0;j<m;++j)
if(ts[j]=='0') b[i]+="2";
else if(ts[j]=='1') b[i]+="3";
else if(ts[j]=='2') b[i]+="0";
else b[i]+="1";
}
else if(y[i]=="?")
{
string ts=b[ma[x[i]]];
for(int j=0;j<m;++j)
if(ts[j]=='0') b[i]+="2";
else if(ts[j]=='1') b[i]+="3";
else if(ts[j]=='2') b[i]+="0";
else b[i]+="1";
}
else
{
string ts=b[ma[x[i]]],ts2=b[ma[y[i]]];
for(int j=0;j<m;++j)
if(ts[j]<='1' && ts2[j]<='1')
b[i]+=(((ts[j]-'0')^(ts2[j]-'0'))+'0');
else if(ts[j]=='0')
{
if(ts2[j]=='2') b[i]+="2";
else b[i]+="3";
}
else if(ts[j]=='1')
{
if(ts2[j]=='2') b[i]+="3";
else b[i]+="2";
}
else if(ts[j]=='2')
{
if(ts2[j]=='0') b[i]+="2";
else if(ts2[j]=='1') b[i]+="3";
else if(ts2[j]=='2') b[i]+="0";
else b[i]+="1";
}
else
{
if(ts2[j]=='0') b[i]+="3";
else if(ts2[j]=='1') b[i]+="2";
else if(ts2[j]=='2') b[i]+="1";
else b[i]+="0";
}
}
}
for(int i=0;i<m;++i)
{
int A=0,B=0;
for(int j=1;j<=n;++j)
if(b[j][i]=='2')
++A;
else if(b[j][i]=='3')
++B;
if(A>=B)
putchar('0');
else
putchar('1');
}
puts("");
for(int i=0;i<m;++i)
{
int A=0,B=0;
for(int j=1;j<=n;++j)
if(b[j][i]=='3')
++A;
else if(b[j][i]=='2')
++B;
if(A>=B)
putchar('0');
else
putchar('1');
}
puts("");
return 0;
}
【推导】【贪心】Codeforces Round #402 (Div. 2) E. Bitwise Formula的更多相关文章
- 【推导】Codeforces Round #402 (Div. 2) A. Pupils Redistribution
一次交换,会让Group A里面的某个数字的数量-1,另一个数字的数量+1:对Group B恰好相反. 于是答案就是xigma(i=1~5,numA[i]-numB[i]>0)(numA[i]- ...
- CF778B(round 402 div.2 E) Bitwise Formula
题意: Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied t ...
- Codeforces Round #402 (Div. 2) A+B+C+D
Codeforces Round #402 (Div. 2) A. Pupils Redistribution 模拟大法好.两个数列分别含有n个数x(1<=x<=5) .现在要求交换一些数 ...
- Codeforces Round #402 (Div. 2)
Codeforces Round #402 (Div. 2) A. 日常沙比提 #include<iostream> #include<cstdio> #include< ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- 贪心 Codeforces Round #301 (Div. 2) B. School Marks
题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
随机推荐
- 近期对于windows服务的理解
1.APP.config的作用 在开发环境下时,根目录下的APP.config里面会填写一些参数之类的.当生成之后,这些参数将会被自动生成在*.exe文件目录中.如图: 其中,.exe文件为Win ...
- POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 20262 Accepted: 7230 ...
- HDU2732:Leapin' Lizards(最大流)
Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- POJ 3104 Drying(二分
Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22163 Accepted: 5611 Descripti ...
- 关于跨域策略文件crossdomain.xml文件--配置实例
转载自:http://bbs.phpchina.com/blog-52440-191623.html 我一直不太明白crossdomain.xml文件是干嘛用的,今天总算比较清楚的知道了一下. 这是F ...
- MySQL实现分页查询
imit 基本实现方式 一般情况下,客户端通过传递 pageNo(页码).pageSize(每页条数)两个参数去分页查询数据库中的数据,在数据量较小(元组百/千级)时使用 MySQL自带的 limit ...
- jQuery获取表格隐藏域与ajax请求数据结合显示详情
0.表格样式
- 自旋锁spin_lock和raw_spin_lock【转】
转自:http://blog.csdn.net/droidphone/article/details/7395983 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 临界区Cr ...
- python基础===pip安装模块失败
此情况只用于网络不畅的安装模块背景: 总出现红色的 Could not find a version that satisfies the requirement pymongo(from versi ...
- Pyhton-Requests之接口测试
非常感谢[百人计划]五娃的分享!下面是整理的笔记: 一.环境准备: Pyhton 2.x或者Pyhton 3.x.Requests库.(我安装的版本是Pyhton 3.4) 安装 Pyhton 3.x ...