【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) = ...
随机推荐
- 【Silverlight】Bing Maps开发应用与技巧一:地图打点与坐标控件(CoordControl)
[Silverlight]Bing Maps开发应用与技巧一:地图打点与坐标控件(CoordControl) 使用Bing Maps Silverlight Control开发中,很多时候都需要实现在 ...
- Bing Maps进阶系列五:通过DeepEarth的MiniMap控件为Bing Maps扩展迷你小地图
Bing Maps进阶系列五:通过DeepEarth的MiniMap控件为Bing Maps扩展迷你小地图 Bing Maps Silverlight Control虽然为我们提供了简洁.方便的开发模 ...
- Flume Netcat Source
1.cd /usr/local2/flume/conf sudo vim netcat.conf # Name the components on this agent a1.sources = r1 ...
- androd基础入门---1环境
1.项目结构特性 2.模拟器设置 3.编译器的下载 直接点击运行即可
- 简单理解jsonp原理
对于javascript程序员来说,发送ajax请求获取后台数据然后把数据和模板拼接成字符串渲染回DOM实现无刷新更新页面这样的操作可谓是轻车熟路.但众所周知,ajax有一个不好,就是不能跨域传输数据 ...
- [Swift通天遁地]二、表格表单-(16)在表单行内嵌入日期和时间拾取器
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- ThreadLocal原理及用法详解
背景 一直以来对ThreadLocal用法模棱两可,不知道怎么用今天好好研究了下给大家分享下. 1.讲解ThreadLocal之前先回顾下什么是取模.x^y.弱引用. 1. 取模运算实际上是计算两数相 ...
- Java数组去重(利用数组,不借助集合)
今天有个同学问我说老师,Java里边数组怎么去重,在不借助List集合的情况下,最后呢我整理了一下,打算发一篇博文,希望能帮助到有用的人,大佬绕过 public static void arrUniq ...
- HyperLedger Fabric部署与链码解读
1.Fabric简介 Fabric是超级账本中的一个项目,用以推进区块链技术.和其他区块链类似,它也有一个账本,使用智能合约,且是一个参与者可以分别管理自身交易的系统.它是一个联盟链.Fabric与其 ...
- 悼念512汶川大地震遇难同胞——老人是真饿了 hdu 2187
在此对 曾经 努力参加 救援的人 致以深深的敬意 . 这一道题 挺简单的 就是简单的 结构体+贪心 而已 不过 用英文 注释 是一个 很大的 进步 , 以后 要习惯 http://acm.hdu ...