【推导】【贪心】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 ...
随机推荐
- Spring源码解析-AutowiredAnnotationBeanPostProcessor
1.实现了BeanPostProcessor接口,可先看这个接口 ApplicationContext可以在自动检测BeanPostProcessor bean,在它创建完后可以创建任何的bean. ...
- 安卓的progress
https://www.cnblogs.com/wolipengbo/archive/2013/10/23/3383667.html
- 移动端list布局,左边固定,右边自适应
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 理解SetCapture、ReleaseCapture、GetCapture(控制了消息发往哪个窗口,是理解消息的关键)
理解SetCapture.ReleaseCapture.GetCapture 正常情况下,鼠标指针位于哪个窗口区域内,鼠标消息就自动发给哪个窗口.如果调用了SetCapture,之后无论鼠标的位置在哪 ...
- ICE学习笔记一----运行官方的java版demo程序
建议新手和我一样,从官网下载英文文档,开个有道词典,慢慢啃. 官方文档下载: http://download.csdn.net/detail/xiong_mao_1/6300631 程序代码就不说了, ...
- php 计算两个日期的间隔天数
使用php内部自带函数实现 1.使用DateTime::diff 实现计算 参考阅读>>PHP DateTime::diff() 上代码: <?php $start = " ...
- MUI 按两次返回键退出应用 及 地理位置获取
<span style="font-size:14px;"><span style="font-size:14px;"> mui.plu ...
- (转载)--SG函数和SG定理【详解】
在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点 ...
- ILSPY反编译工具下载代替收费的Reflector工具
原文发布时间为:2011-10-10 -- 来源于本人的百度文章 [由搬家工具导入] ILSPY反编译工具下载 http://build.sharpdevelop.net/BuildArtefacts ...
- Linux ssh的使用
1.查看SSH客户端版本 有的时候需要确认一下SSH客户端及其相应的版本号.使用ssh -V命令可以得到版本号.需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用Op ...