【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) = ...
随机推荐
- Spark SQL读parquet文件及保存
import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.sql.{Row, SparkSession} im ...
- PCB 加投率计算实现基本原理--K最近邻算法(KNN)
PCB行业中,客户订购5000pcs,在投料时不会直接投5000pcs,因为实际在生产过程不可避免的造成PCB报废, 所以在生产前需计划多投一定比例的板板, 例:订单 量是5000pcs,加投3%,那 ...
- eclipse ------validation功能
今天来讲讲eclipse 的validation功能,该功能用于检测代码存在的“潜在”问题,比如:JSP文件的语法错误,XML中的schema错误等,这么好的功能还有什么可讲的呢,我今天要讲的是val ...
- php实现下载
PHP实现下载文件的两种方法.分享下,有用到的朋友看看哦. 方法一: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php /** * 下载文件 * ...
- [Swift通天遁地]九、拔剑吧-(15)搭建具有滑出、视差、3D变形等切换效果的引导页
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- selenium3 + python - autoit上传文件
一.环境准备: 1.可以autoit官网上下载,安装 http://www.autoitscript.com/site/ 2.AutoIt里面几个菜单功能介绍: SciTE Script Editor ...
- C#用Microsoft.Office.Interop.Word进行Word转PDF的问题
之前用Aspose.Word进行Word转PDF发现'\'这个字符会被转换成'¥'这样的错误,没办法只能换个方法了.下面是Microsoft.Office.Interop.Word转PDF的方法: p ...
- centos7安装mysql和mysql-connector-c++
最近为了搭建自己的开发环境,又一次在centos7上面开始安装mysql和c++的访问环境,特此记录一下搭建过程,方便以后查阅 一.安装mysql centos7 默认安装了mariaDB,导致不能安 ...
- 研磨JavaScript系列(一):回归简单
想要理解JavaScript,你得首先放下对象和类的概念,回到数据和代码的本原.编程世界只有数据和代码两种基本元素,而这两种元素又有着纠缠不清的关系.JavaScript就是把数据和代码都简化到最原始 ...
- [hihocoder][Offer收割]编程练习赛62
方向 #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JUDGE #include &qu ...