Codeforces Round #344 (Div. 2) A. Interview【水题】
来源:互联网 发布:北京鹰目网络 编辑:程序博客网 时间:2024/05/16 13:00
Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem.
We define function f(x, l, r) as a bitwise(按位) OR of integers xl, xl + 1, ..., xr, where xi is the i-th element of the array x. You are given two arrays a and b of length n. You need to determine the maximum value of sum f(a, l, r) + f(b, l, r)among all possible 1 ≤ l ≤ r ≤ n.
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the length of the arrays.
The second line contains n integers ai (0 ≤ ai ≤ 109).
The third line contains n integers bi (0 ≤ bi ≤ 109).
Print a single integer — the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.
51 2 4 3 22 3 3 12 1
22
1013 2 7 11 8 4 9 8 5 15 7 18 9 2 3 0 11 8 6
46
Bitwise(按位) OR of two non-negative integers a and b is the number c = a OR b, such that each of its digits in binary notation is 1 if and only if at least one of a or b have 1 in the corresponding position in binary notation.
In the first sample, one of the optimal answers is l = 2 and r = 4, becausef(a, 2, 4) + f(b, 2, 4) = (2 OR 4 OR 3) + (3 OR 3 OR 12) = 7 + 15 = 22. Other ways to get maximum value is to choose l = 1 and r = 4, l = 1 and r = 5, l = 2 and r = 4, l = 2 and r = 5, l = 3 and r = 4, or l = 3 and r = 5.
In the second sample, the maximum value is obtained for l = 1 and r = 9.
由按位或运算规则可知,连续或运算得出的数值是递增的,故只需从第一个或运算到最后一个即可达到最大值
<pre name="code" class="cpp">#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<iomanip>#include<cmath>using namespace std;int main(){ long long i,n,a,b,ansa=0,ansb=0; scanf("%I64d",&n); for(i=1;i<=n;i++) { scanf("%I64d",&a); ansa=ansa|a; } for(i=1;i<=n;i++) { scanf("%I64d",&b); ansb=ansb|b; } printf("%I64d\n",ansa+ansb); return 0;}
- Codeforces Round #344 (Div. 2) A. Interview【水题】
- Codeforces Round #344 (Div. 2) 631A Interview (DP)
- Codeforces Round #344 (Div. 2)-A. Interview(前缀和)
- Codeforces Round #344 (Div. 2) A. Interview (位运算)
- Round #344 (Div. 2) A - Interview
- Codeforces Round #344 (Div. 2)A
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 – Elimination Round 2) A. Interview wi
- Codeforces Round #380A - Interview with Oleg
- Codeforces Round #344 (Div. 2) A 题题解 (暴力)
- <Codeforces Round #147 (Div. 2)>A. Free Cash(水题)
- Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)
- Codeforces Round #261 (Div. 2) A. Pashmak and Garden (水题)
- Codeforces Round #278 (Div. 2) A. Giga Tower(水题)
- Codeforces Round #182 (Div. 2)A(水题)
- [水题]Codeforces Round #307 (Div. 2) A.GukiZ and Contest
- A. Lineland Mail-水题-Codeforces Round #Pi (Div. 2)
- Codeforces Round #335 (Div. 2) A. Magic Spheres 水题
- Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)解题报告
- Kaggle(2):验证和过分拟合
- 关于aop环绕通知的几点理解
- 关于无法解析布局文件和id的问题
- Java集合小结(1)
- SoundPool与mediaPlayer的使用
- Codeforces Round #344 (Div. 2) A. Interview【水题】
- LeetCode 2.Add Two Numbers
- HDU 1040 As Easy As A+B
- c++上机实验报告项目1-3
- tinyos学习之day 2
- 周例会的作用
- webkit
- The Swift Programming Language 中英文双语版
- 从一个问题看MySQL的权限访问控制