Codeforces Round #344 (Div. 2)(按位或运算)
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.
5
1 2 4 3 2
2 3 3 12 1
22
10
13 2 7 11 8 4 9 8 5 1
5 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, because f(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.
题意(虽然题目很水,不过位运算太弱了,还是写了一下):
函数f(a, l, r)表达式为:al | al+1 | al+2 | ...... | ar,其中a为一个集合,ai表示集合a中第i个元素;
现有集合a , b (都有n个元素),求f(a, l, r) + f(b, l , r)的最大值;
思路:首先我想到的就是两个for找l和r,因为数据只有1000,肯定是不会超时的;
不过我对位运算并不熟悉,所以先按样例解析跑了一遍。。
发现2 | 4 | 3 = 1 | 2 | 4 | 3 = 2 | 4 | 3 | 2 = 1 | 2 | 4 | 3 | 2 !!!!很神奇有木有!!!!
由此猜测对任意一个集合的所有子集的所有按位或运算。。最大值等于这个集合本身所有元素间进行位运算。。
样例2也符合这个规律。。。
仔细一想。。不难发现任意两个数进行位或运算后得到的值必然是非减的。。这可以由位或运算的规则证明。。
位或其功能是参与运算的两数各对应的二进位相或。只要对应的二个二进位有一个为1时,结果位就为1。2进制对应位只要有一个为1,其结果就是1.。所有运算结果不可能比原数小。。。
所有这个题目只需要分别把两个集合的元素进行位或运算再相加就可以了啦。。。
代码:
#include <bits/stdc++.h>
#define ll long long
#define MAXN 100000+10
using namespace std; int main(void)
{
int n, x, y, cnt1=, cnt2=; // 注意cnt1, cnt2赋初值0,如果赋值1的话其结果可能变大
cin >> n;
for(int i=; i<n; i++)
{
cin >> x;
cnt1|=x;
}
for(int j=; j<n; j++)
{
cin >> y;
cnt2|=y;
}
cout << cnt1+cnt2 << endl;
return ;
}
Codeforces Round #344 (Div. 2)(按位或运算)的更多相关文章
- Codeforces Round #344 (Div. 2) A
A. Interview time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #344 (Div. 2) A. Interview
//http://codeforces.com/contest/631/problem/Apackage codeforces344; import java.io.BufferedReader; i ...
- Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳
E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...
- Codeforces Round #344 (Div. 2) D. Messenger kmp
D. Messenger 题目连接: http://www.codeforces.com/contest/631/problem/D Description Each employee of the ...
- Codeforces Round #344 (Div. 2) C. Report 其他
C. Report 题目连接: http://www.codeforces.com/contest/631/problem/C Description Each month Blake gets th ...
- Codeforces Round #344 (Div. 2) B. Print Check 水题
B. Print Check 题目连接: http://www.codeforces.com/contest/631/problem/B Description Kris works in a lar ...
- Codeforces Round #344 (Div. 2) A. Interview 水题
A. Interview 题目连接: http://www.codeforces.com/contest/631/problem/A Description Blake is a CEO of a l ...
- Codeforces Round #443 (Div. 2) C 位运算
C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #344 (Div. 2) B. Print Check
B. Print Check time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- eclipse emacs
eclipse emacs 插件 http://www.mulgasoft.com/emacsplus eclipse字体设置: 一.把字体设置为Courier New 操作步骤:打开Elcipse ...
- .assetbundle 和.unity3d 好处
.assetbundle 资源文件 .unity3D 场景文件 xml.json 静态存储和 还原 AssetBuddle 优点:减小压缩包.资源更新.分开安装包和数据包.AssetBuddle加密 ...
- 导航栏的坑 (导航透明/导航除线/titleIView)
//以下四个条件缺一不可 /.必须是半透明状态 self.navigationBar.translucent = YES; //2.导航栏背景图片为空图片 (不可以设置backgroundColor或 ...
- Android学习笔记(十五)——实战:强制下线
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 实现强制下线功能的思路也比较简单,只需要在界面上弹出一个对话框, 让用户无法进行任何其他操作, 必须要点击对话 ...
- SNMP报文抓取与分析(一)
SNMP报文抓取与分析(一) 1.抓取SNMP报文 SNMP报文的形式大致如下图所示 我们这里使用netcat这个工具来抓取snmp的PDU(协议数据单元).(因为我们并不需要前面的IP和UDP首部) ...
- django的序列化
关于django的序列化主要应用在将数据库中检索的数据返回给客户端用户,特别的Ajax请求一般返回的Json格式 两种方法: 方法一:serializers: 缺点就是只能应用于对象 "&q ...
- c# winform UI + python底层的一点尝试
鉴于python做winform之类的UI比较弱.于是想结合C#的winform 和 python的底层开发(windows平台). 尝试做了一个RSS阅读器.在这里:http://download. ...
- phpcms分页用法简介
PHPCMS分页的用法 前面需要有引用的list,代码如下: {pc:content action="lists" catid="11" order=" ...
- devstack meaning of: n-cond, n-novnc and n-xvnc
devstack has shortened names for a number of services, e.g. g-api = glance api g-reg = glance regist ...
- Silverlight datagrid 排序 (转)
Silverlight的DataGrid有很多强大之处,其中一个便是排序. DataGrid指定过ItemsSource之后,通过点击列头就可以实现排序,不用写任何代码.这对我这种懒人来说实在是太爽了 ...