【推导】【贪心】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 ...
随机推荐
- [poj 3281]最大流+建图很巧妙
题目链接:http://poj.org/problem?id=3281 看了kuangbin大佬的思路,还用着kuangbin板子orz http://www.cnblogs.com/kuangb ...
- HDU 多校对抗赛 D Distinct Values
Distinct Values Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- DSP投放进阶指南
- Js+CSS3实现手风琴效果
效果截图: HTML代码: <div id="container"> <img src="images/photo01.jpg" alt=&q ...
- Emmet 也有快速生成文件头的功能
输入下边加粗的缩写,然后Tab,就OK了http://docs.emmet.io/cheat-sheet/ html:4t <!DOCTYPE HTML PUBLIC "-//W3C/ ...
- 应对ubuntu linux图形界面卡住的方法
有的时候,我的ubuntu图形界面会卡住,当然这个时候你可以重新启动,不过最好的办法应该是结束这个桌面进程 那桌面卡住了怎么来结束桌面进程呢? 这时候就需要打开tty了 按下键盘ctrl+alt+f1 ...
- POJ2495(棋盘分治,染色)
Incomplete chess boards Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2483 Accepted ...
- kuangbin带你飞 并查集 题解
做这套题之前一直以为并查集是很简单的数据结构. 做了才发现自己理解太不深刻.只看重片面的合并集合.. 重要的时发现每个集合的点与这个根的关系,这个关系可以做太多事情了. 题解: POJ 2236 Wi ...
- 使用MXNet远程编写卷积神经网络用于多标签分类
最近试试深度学习能做点什么事情.MXNet是一个与Tensorflow类似的开源深度学习框架,在GPU显存利用率上效率高,比起Tensorflow显著节约显存,并且天生支持分布式深度学习,单机多卡.多 ...
- mysql的expain(zz)
两张表,T1和T2,都只有一个字段,id int.各插入1000条记录,运行如下语句: explain SELECT t1.id,t2.id FROM t1 INNER JOIN t2 ON t1.i ...