【codeforces 779E】Bitwise Formula
【题目链接】:http://codeforces.com/contest/779/problem/E
【题意】
给你n个长度为m的二进制数
(有一些是通过位运算操作两个数的形式给出);
然后有一个未知数字,用符号’?’表示;
(所有的数字都是长度为m的二进制数);
然后让你确定这个’?’使得n个数字的和最大、最小;
【题解】
模拟题;
会有嵌套的情况;
即
a=x & y
b=a&?
类似这样.
所以在算b之前,先把a的值确定下来;
明白了怎么算之后;
枚举那个未知数字的m位的每一位;
每一位都只有两种可能,即为0或者为1;
假设为0;
然后带进去算一下最后结果,这一位的和为多少;
假设为1
然后带进去算一下最后结果,这一位的和为多少;
如果要最大值就选那个大的对应的数字,最小值则相反。
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int M = 1e3+100;
const int N = 5e3 + 100;
struct abc
{
int a, b, p;
int sz[M];
};
int n, m,change,f[N];
string name,s;
map <string, int> dic;
abc a[N];
vector<int>v1, v2;
int get_val(int pos)
{
int sum = 0;
f[0] = change;
rep1(i, 1, n)
{
if (a[i].p == 0)
{
sum += a[i].sz[pos];
f[i] = a[i].sz[pos];
continue;
}
int x = f[a[i].a], y = f[a[i].b];
int p = a[i].p;
if (p == 1)
f[i] = x&y;
if (p == 2)
f[i] = x | y;
if (p == 3)
f[i] = x^y;
sum += f[i];
}
return sum;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n), rei(m);
dic["?"] = 0;
rep1(i, 1, n)
{
cin >> name; dic[name] = i;
cin >> s; cin >> s;
if (s[0] == '1' || s[0] == '0')
{
a[i].p = 0;
rep1(j, 1, m)
a[i].sz[j] = s[j - 1]-'0';
}
else
{
a[i].a = dic[s];
cin >> s;
if (s[0] == 'A') a[i].p = 1;
if (s[0] == 'O') a[i].p = 2;
if (s[0] == 'X') a[i].p = 3;
cin >> s;
a[i].b = dic[s];
}
}
rep1(i, 1, m)
{
change = 0; int temp0 = get_val(i);
change = 1; int temp1 = get_val(i);
if (temp0 <= temp1)
v1.ps(0);
else
v1.ps(1);
if (temp0 >= temp1)
v2.ps(0);
else
v2.ps(1);
}
rep1(i, 0, m - 1)
printf("%d", v1[i]);
puts("");
rep1(i, 0, m - 1)
printf("%d", v2[i]);
puts("");
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 779E】Bitwise Formula的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 742B】Arpa’s obvious problem and Mehrdad’s terrible solution
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【21.37%】【codeforces 579D】"Or" Game
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- git的基本操作流程
1.git clone 初始会有默认的master分支,并且master和origin/master自动建立了映射关系 2. git checkout -b local 创建并且切换到local ...
- poj3539 Elevator——同余类bfs
题目:http://poj.org/problem?id=3539 题目大意是给定 a, b, c,求 1~h 内有多少个数可以被 a, b, c 通过加减法组成: 这是今天刚讲的神奇的——同余类 b ...
- JSP-Runoob:JSP 点击量统计
ylbtech-JSP-Runoob:JSP 点击量统计 1.返回顶部 1. JSP 点击量统计 有时候我们需要知道某个页面被访问的次数,这时我们就需要在页面上添加页面统计器,页面访问的统计一般在用户 ...
- Linux进程状态解析
引言 Linux是一个多用户,多任务的系统,可以同时运行多个用户的多个程序,就必然会产生很多的进程,而每个进程会有不同的状态. 在下文将对进程的R.S.D.T.Z.X 六种状态做个说明. PROCE ...
- Kafka详解与总结(四)
Kafka消息分发和消费者push.pull机制 1. 消息分发 Producer客户端负责消息的分发 kafka集群中的任何一个broker都可以向producer提供metadata信息,这些me ...
- BZOJ 4811 树链剖分+线段树
思路: 感觉这题也可神了.. (还是我太弱) 首先发现每一位不会互相影响,可以把每一位分开考虑,然后用树链剖分或者LCT维护这个树 修改直接修改,询问的时候算出来每一位填0,1经过这条链的变换之后得到 ...
- [转]linux tr命令详解
转自:http://www.cnblogs.com/huangxingkezhan/archive/2013/01/23/2874031.html 通过使用 tr,您可以非常容易地实现 sed 的许多 ...
- Jquery 实现列表的显示和隐藏
本人github源码下载地址:https://github.com/liuyanpeng521/ListChange.git
- MVC系列学习(三)-EF的延迟加载
1.什么叫延迟加载 字面上可以理解为,一个动作本该立即执行的动作,没有立即执行 2.从代码上理解 static void Main(string[] args) { //执行该语句的时候,查看sql监 ...
- B树、B+树、红黑树、AVL树比较
B树是为了提高磁盘或外部存储设备查找效率而产生的一种多路平衡查找树. B+树为B树的变形结构,用于大多数数据库或文件系统的存储而设计. B树相对于红黑树的区别 在大规模数据存储的时候,红黑树往往出现由 ...