Codeforces Round #344 (Div. 2) A. Interview (位运算)
来源:互联网 发布:下载伴奏的软件 编辑:程序博客网 时间:2024/05/19 00:13
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 andr = 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.
题解:对任意一个集合的所有子集的所有按位或运,最大值等于这个集合本身所有元素间进行位运算。因为元素与元素之间的或运算得到的结果都是非递减的。或嘛,1|1=1;1|0=1。。。不是很明显吗。。。
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")//#include<bits/stdc++.h>#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<cstring>#include<vector>#include<map>#include<cmath>#include<queue>#include<set>#include<stack>#include <utility> using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int,int> pii;typedef vector<int> vi;#define mst(a) memset(a, 0, sizeof(a))#define M_P(x,y) make_pair(x,y)#define in freopen("in.txt","r",stdin) #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 const int lowbit(int x) { return x&-x; } const double eps = 1e-8; const int INF = 1e9+7; const ll inf =(1LL<<62) ;const int MOD = 1e9+7; const ll mod = (1LL<<32);const int N =2e5+7; const int M=100010;const ll MAX=1e18;//const int maxn=1001; template <class T1, class T2>inline void getmax(T1 &a, T2 b) {if (b>a)a = b;} template <class T1, class T2>inline void getmin(T1 &a, T2 b) {if (b<a)a = b;}int read(){int v = 0, f = 1;char c =getchar();while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();return v*f;}int main(){int a=0,b=0;int n;cin>>n;int x;for(int i=0;i<n;i++){cin>>x; a|=x;}for(int i=0;i<n;i++){cin>>x; b|=x;}cout<<a+b;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【水题】
- Codeforces--631A--Interview(位运算)
- Codeforces Round #443 (Div. 2) A、B、C 位运算
- Codeforces Round #195 (Div. 2)位运算
- Codeforces Round #238 (Div. 2)(位运算)
- Round #344 (Div. 2) A - Interview
- Codeforces 631A Interview(分析&&位运算| )
- CodeForces 631A:Interview【位运算】
- Codeforces Round #112 (Div. 2) E题 位运算
- Codeforces Round #306 (Div. 2) B.Preparing Olympiad (位运算)
- Codeforces Round #267 (Div. 2) B. Fedor and New Game(位运算)
- Codeforces Round #344 (Div. 2)A
- Codeforces Round #344 (Div. 2) A 题题解 (暴力)
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 – Elimination Round 2) A. Interview wi
- CF631 A. Interview【位运算】
- java 23种设计模式
- HTTP协议与HTTPS的区别
- Spark读写S3(AWS亚马逊中国区beijing)
- JavaScript趣题:创建电话号码
- Statement、PreparedStatement、CallableStatement的作用
- Codeforces Round #344 (Div. 2) A. Interview (位运算)
- Oracle 删除表空间错误 提示:ora-02429:无法删除用于强制唯一/主键的索引
- ORA-00922选项缺失或无效
- JS 格式化时间
- 什么函数不能声明为虚函数
- java 开发环境配置
- 多行输入多行输出-购买商品-京东2017笔试编程
- 代码实现UI6宫格布局
- 【JAVA基础小问题】"=="和equals方法有什么区别?