【题目链接】:http://codeforces.com/contest/779/problem/E

【题意】



给你n个长度为m的二进制数

(有一些是通过位运算操作两个数的形式给出);

然后有一个未知数字,用符号’?’表示;

(所有的数字都是长度为m的二进制数);

然后让你确定这个’?’使得n个数字的和最大、最小;

【题解】



模拟题;

会有嵌套的情况;



a=x & y

b=a&?

类似这样.

所以在算b之前,先把a的值确定下来;

明白了怎么算之后;

枚举那个未知数字的m位的每一位;

每一位都只有两种可能,即为0或者为1;

假设为0;

然后带进去算一下最后结果,这一位的和为多少;

假设为1

然后带进去算一下最后结果,这一位的和为多少;

如果要最大值就选那个大的对应的数字,最小值则相反。



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define ps push_back
  10. #define fi first
  11. #define se second
  12. #define rei(x) scanf("%d",&x)
  13. #define rel(x) scanf("%lld",&x)
  14. #define ref(x) scanf("%lf",&x)
  15. typedef pair<int, int> pii;
  16. typedef pair<LL, LL> pll;
  17. const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
  18. const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
  19. const double pi = acos(-1.0);
  20. const int M = 1e3+100;
  21. const int N = 5e3 + 100;
  22. struct abc
  23. {
  24. int a, b, p;
  25. int sz[M];
  26. };
  27. int n, m,change,f[N];
  28. string name,s;
  29. map <string, int> dic;
  30. abc a[N];
  31. vector<int>v1, v2;
  32. int get_val(int pos)
  33. {
  34. int sum = 0;
  35. f[0] = change;
  36. rep1(i, 1, n)
  37. {
  38. if (a[i].p == 0)
  39. {
  40. sum += a[i].sz[pos];
  41. f[i] = a[i].sz[pos];
  42. continue;
  43. }
  44. int x = f[a[i].a], y = f[a[i].b];
  45. int p = a[i].p;
  46. if (p == 1)
  47. f[i] = x&y;
  48. if (p == 2)
  49. f[i] = x | y;
  50. if (p == 3)
  51. f[i] = x^y;
  52. sum += f[i];
  53. }
  54. return sum;
  55. }
  56. int main()
  57. {
  58. //freopen("F:\\rush.txt", "r", stdin);
  59. rei(n), rei(m);
  60. dic["?"] = 0;
  61. rep1(i, 1, n)
  62. {
  63. cin >> name; dic[name] = i;
  64. cin >> s; cin >> s;
  65. if (s[0] == '1' || s[0] == '0')
  66. {
  67. a[i].p = 0;
  68. rep1(j, 1, m)
  69. a[i].sz[j] = s[j - 1]-'0';
  70. }
  71. else
  72. {
  73. a[i].a = dic[s];
  74. cin >> s;
  75. if (s[0] == 'A') a[i].p = 1;
  76. if (s[0] == 'O') a[i].p = 2;
  77. if (s[0] == 'X') a[i].p = 3;
  78. cin >> s;
  79. a[i].b = dic[s];
  80. }
  81. }
  82. rep1(i, 1, m)
  83. {
  84. change = 0; int temp0 = get_val(i);
  85. change = 1; int temp1 = get_val(i);
  86. if (temp0 <= temp1)
  87. v1.ps(0);
  88. else
  89. v1.ps(1);
  90. if (temp0 >= temp1)
  91. v2.ps(0);
  92. else
  93. v2.ps(1);
  94. }
  95. rep1(i, 0, m - 1)
  96. printf("%d", v1[i]);
  97. puts("");
  98. rep1(i, 0, m - 1)
  99. printf("%d", v2[i]);
  100. puts("");
  101. //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
  102. return 0;
  103. }

【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. POJ1061 青蛙的约会 exgcd

    这个题虽然很简单,但是有一个比较坑的地方,就是gcd不一定是1,有可能是别的数.所以不能return 1,而是return a; 题干: Description 两只青蛙在网上相识了,它们聊得很开心, ...

  2. bzoj1009 [HNOI2008]GT考试——KMP+矩阵快速幂优化DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 字符串计数DP问题啊...连题解都看了好多好久才明白,别提自己想出来的蒟蒻我... 首 ...

  3. El Dorado(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=2372 题意:给出n个数,求长度为m的递增子序列的数目. 思路:状态转移方程 dp[i][j] = sum(dp[ ...

  4. discuz的cutstr函数

    function cutstr($string, $length, $dot = ' ...') { if(strlen($string) <= $length) { return $strin ...

  5. Ansi2Utf8 小工具

    将GB2312编码的文件转成Unity使用的UTF8无bom格式 主要用批处理执行 Ansi2Utf8.exe XXXXX.txt  源代码 using System; using System.Co ...

  6. Java学习-异常2

    1.异常处理的第一种方式是:上抛[throws] 2.异常处理的第二种方式是:try....catch..如果不想让调用程序知道该异常发生了,被调用的程序应该使用try...catch..进行异常捕捉 ...

  7. Ajax实现文件的上传

    Ajax实现文件的上传 准备 ajax的参数补充 type不写的话默认是GET dataType和ContentType: dataType: 浏览器发给服务器希望返回的数据类型 .. 如果明确地指定 ...

  8. CSS怎样改变行内样式(通过外部级联样式表) css !important用法CSS样式使用优先级判断

    CSS样式优先级 行内>内部>外部 使用!important的css定义是拥有最高的优先级的.只是在ie6下出了一点小的bug,注意书写方式一般可以轻松避开的. CSS中的!importa ...

  9. Elasticsearch之CURL命令的DELETE

    也可以看我写的下面的博客 Elasticsearch之curl删除 Elasticsearch之curl删除索引库 删除,某一条数据,如下 [hadoop@master elasticsearch-] ...

  10. [转]Window2008站点安全设置,IIS7/IIS7.5中目录执行权限的设置方法

    本文转自:http://blog.snsgou.com/post-510.html 最近帮一个朋友管理Window 2008服务器,发现有个站点是用asp写的,更可怕的是还有传说中的“上传漏洞”,在上 ...