【推导】【贪心】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 ...
随机推荐
- 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 网络预选赛 A
Problem 1603 - Minimum Sum Time Limit: 2000MS Memory Limit: 65536KB Total Submit: 564 Accepted: ...
- 毕业答辩的PPT攻略
关于内容: 1.一般概括性内容:课题标题.答辩人.课题执行时间.课题指导教师.课题的归属.致谢等. 2.课题研究内容:研究目的.方案设计(流程图).运行过程.研究结果.创新性.应用价值.有关课题延续 ...
- Docker Community Edition for CentOS
Docker CE for CentOS Docker CE for CentOS distribution is the best way to install the Docker platfor ...
- TypeConverter使用
如下代码, <Window.Resources> <local:Human x:Key="human" Name="Tester1" Chil ...
- COGS2085 Asm.Def的一秒
时间限制:1 s 内存限制:256 MB [题目描述] “你们搞的这个导弹啊,excited!” Asm.Def通过数据链发送了算出的疑似目标位置,几分钟后,成群结队的巡航导弹从“无蛤”号头顶掠过 ...
- MyBatis系列四 之 智能标签进行查询语句的拼接
MyBatis系列四 之 智能标签进行查询语句的拼接 使用Foreach进行多条件查询 1.1 foreach使用数组进行多条件查询 在MyBatis的映射文件中进行如下配置 <!--根据数组进 ...
- js获取上个月日期
javascript根据当前日期获取上个月日期 function lastMonthDate(){ var Nowdate = new Date(); var vYear = Nowdate.getF ...
- webdriver函数
import sys; print('%s %s' % (sys.executable or sys.platform, sys.version)) PyDev console: starting. ...
- url 拼接的一个模块furl
from furl import furl getlongtexturl="https://weibo.com/p/aj/mblog/getlongtext" params={ & ...
- 白话TCP三次握手
在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. 第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认: 第二次握 ...