CF778B(round 402 div.2 E) Bitwise Formula
题意:
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 mbits. 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.
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:
- Binary number of exactly m bits.
- 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.
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.
3 3
a := 101
b := 011
c := ? XOR b
011
100
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
0
0
思路:
1.由于是位操作,所以各个位之间的运算结果相互独立。因此一位一位割裂来计算,选取每一位取“0”还是取“1”更好一点。对于每一位来讲,n个数的这一位上的“1”的个数的和越多,则所有数的和越大。另外,如果取“0”和取“1”能获得相同的结果,则取“0”即可。
2.学习使用bitset用法。
实现:
#include <iostream>
#include <cstdio>
#include <bitset>
#include <map>
using namespace std; int n, m, res[][];
bitset<> bs[][];
map<string, int> mp;
int main()
{
mp["?"] = ;
bs[][].set();
cin >> n >> m;
for (int i = ; i < n; i++)
{
string x, tmp, y, op, z;
cin >> x >> tmp >> y;
mp[x] = i + ;
if (y[] == '' || y[] == '')
{
for (int j = ; j < m; j++)
{
bs[i + ][].set(j, y[j] - '');
bs[i + ][].set(j, y[j] - '');
}
}
else
{
cin >> op >> z;
for (int j = ; j <= ; j++)
{
if (op == "AND")
{
bs[i + ][j] = bs[mp[y]][j] & bs[mp[z]][j];
}
else if (op == "OR")
{
bs[i + ][j] = bs[mp[y]][j] | bs[mp[z]][j];
}
else
{
bs[i + ][j] = bs[mp[y]][j] ^ bs[mp[z]][j];
}
}
}
for (int j = ; j < m; j++)
{
for (int k = ; k <= ; k++)
res[j][k] += bs[i + ][k][j];
}
}
for (int i = ; i < m; i++)
{
cout << (res[i][] > res[i][] ? : );
}
puts("");
for (int i = ; i < m; i++)
{
cout << (res[i][] >= res[i][] ? : );
}
return ;
}
CF778B(round 402 div.2 E) Bitwise Formula的更多相关文章
- 【推导】【贪心】Codeforces Round #402 (Div. 2) E. Bitwise Formula
按位考虑,每个变量最终的赋值要么是必为0,要么必为1,要么和所选定的数相同,记为2,要么和所选定的数相反,记为3,一共就这四种情况. 可以预处理出来一个真值表,然后从前往后推导出每个变量的赋值. 然后 ...
- Codeforces Round #402 (Div. 2)
Codeforces Round #402 (Div. 2) A. 日常沙比提 #include<iostream> #include<cstdio> #include< ...
- 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) A,B,C,D,E
A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces Round #402 (Div.2) A-E
2017.2.26 CF D2 402 这次状态还算能忍吧……一路不紧不慢切了前ABC(不紧不慢已经是在作死了),卡在D,然后跑去看E和F——卧槽怎么还有F,早知道前面做快点了…… F看了看,不会,弃 ...
- Codeforces Round #402 (Div. 1)
A题卡壳了,往离线倒着加那方面想了会儿,后来才发现方向错了,二十多分钟才过掉,过了B后做D,想法好像有点问题,最后只过两题,掉分了,差一点回紫. AC:AB Rank:173 Rating:2227- ...
- Codeforces Round#402(Div.1)掉分记+题解
哎,今天第一次打div1 感觉头脑很不清醒... 看到第一题就蒙了,想了好久,怎么乱dp,倒过来插之类的...突然发现不就是一道sb二分吗.....sb二分看了二十分钟........ 然后第二题看了 ...
- Codeforces Round #402 (Div. 2) A B C sort D二分 (水)
A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 【DFS】Codeforces Round #402 (Div. 2) B. Weird Rounding
暴搜 #include<cstdio> #include<algorithm> using namespace std; int n,K,Div=1,a[21],m,ans=1 ...
随机推荐
- 从零开始徒手撸一个vue的toast弹窗组件
相信普通的vue组件大家都会写,定义 -> 引入 -> 注册 -> 使用,行云流水,一气呵成,但是如果我们今天是要自定义一个弹窗组件呢? 首先,我们来分析一下弹窗组件的特性(需求): ...
- easyui 日期范围前后台的设置以及实现
1.页面部分(引入相应的js) <td class="w40 tl pl10">从日期:</td> <td> <input class=& ...
- 通过Toad工具查看dmp里面的表
今天有同事要查看dmp里面的表是否有数据,虽然可以把单表数据通过exp导出查看,但还是稍显有点麻烦,要花时间. 无意中发现toad工具可以直接查看dmp里面的表数据. 第一步:Database--&g ...
- 计算机科学 —— 时间戳(timestamp)
时间戳的一个重要属性即是:唯一性,以起到唯一标识的作用: 1. linux 命令行 $ date +%s 1506222745 2. Python 时间戳 内置 time 库 >> tim ...
- 官网下载java相关资源
官网下载java相关资源 官网地址:http://www.oracle.com 一.下载JDK 1.首先进入Downloads >> Java For Developers,如图 2.点击 ...
- 漫谈WebQQ 协议
阅读目录 1,WEBQQ的登陆协议 2,传说中的心跳包 3,获得群,好友, 4实战(盗号-外挂-广告) 要说怎么突然研究起WEBQQ,也是比较偶然的机会,因为前一份工作专注于B2 ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 24. Logging
常用的诊断中间件: UseDeveloperExceptionPage UseStatusCodePages:返回 400~600 的状态码 UseExceptionHandler 自定义异常的处理器 ...
- 任务47:Identity MVC:ReturnUrl实现
任务47:Identity MVC:ReturnUrl实现 在最上面加一个私有的方法 登陆也加上returnUrl Login的post方法.加入returnUrl的参数 登陆界面也需要加上 asp- ...
- 洛谷 - P1801 - 黑匣子 - 对顶堆
这道题是提高+省选-的难度,做出来的话对数据结构题目的理解会增加很多. 可以使用一种叫做对顶堆的东西,对顶堆是在线维护第n小的logn的算法.大概的思路是,假如我们要找的是第n小,我们就维护一个大小为 ...
- hdoj1027【STL系列。。。?】
这个太夸张了...感觉是有别的方法,但是觉得再说吧...以后碰到全排列应该也是用STL嗨的吧...嗯,,,就是这样的....?再说,再说.. 还有杭电支持c艹11,很棒 #include <bi ...