【题目链接】: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的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【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 ...

  3. 【21.37%】【codeforces 579D】"Or" Game

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  5. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  6. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  7. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  8. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  9. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

随机推荐

  1. 如何理解Apache License, Version 2.0(整理)

    如何理解Apache License, Version 2.0(整理) 问题: 最近看到apache发布了2.0版本的License.而且微软也以此发布了部分源代码.我对OpenSource不是特熟, ...

  2. P3202 [HNOI2009]通往城堡之路 神仙题

    这个题不是坑人吗...写个tarjan标签,然后拿这么个神仙题来搞...代码有点看不懂,有兴趣的可以去洛谷题解区看看,懒得想了. 题干: 题目描述 听说公主被关押在城堡里,彭大侠下定决心:不管一路上有 ...

  3. atoi函数——将字符串转换为整数

    atoi在一个叫<cstdlib>的库里,可以把字符串直接转换为整数,贼强势. 还有一个atof,就是换成浮点数,实质上是一样的. 例子: #include<cstdlib> ...

  4. 洛谷P1281 书的复制

    题目描述 现在要把m本有顺序的书分给k给人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如不能把第一.第三.第四本书给同一个人抄写. ...

  5. DVB-subtitle解析流程浅

    DTV包含SUBTITLE和TTX. PMT中分别有不同的描述符对应,如下图的TTX descripter=0x56.语言ISO-639="fin" subtitle descri ...

  6. codevs2147数星星(哈希)

    2147 数星星  时间限制: 3 s  空间限制: 64000 KB  题目等级 : 钻石 Diamond   题目描述 Description 小明是一名天文爱好者,他喜欢晚上看星星.这天,他从淘 ...

  7. P2251 质量检测(ST表)

    P2251 质量检测 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后统计前M件产品中质量最差的产品的分值Q[m] = min{A1, A2, ... ...

  8. 使用Keras做OCR时报错:ValueError: Tensor Tensor is not an element of this graph

    现象 项目使用 Flask + Keras + Tensorflow 同样的代码在机器A和B上都能正常运行,但在机器C上就会报如下异常.机器A和B的环境是先安装的,运行.调试成功后才尝试在C上跑. F ...

  9. Win7 + VS2015 + Python3.6编译

    0. 下载安装hg. http://bitbucket.org/tortoisehg/files/downloads/tortoisehg-4.0.1-x64.msi 1. 下载Python3.6源代 ...

  10. ACM_Mystery

    Mystery Time Limit: 2000/1000ms (Java/Others) Problem Description: No Description Input: The first l ...