Codeforces 578B "Or" Game (前缀和 + 贪心)
Codeforces Round #320 (Div. 1) [Bayan Thanks-Round]
题目链接:B. "Or" Game
You are given \(n\) numbers \(a_1, a_2, ..., a_n\). You can perform at most \(k\) operations. For each operation you can multiply one of the numbers by \(x\). We want to make \(a_1 | a_2 | ... | a_n\) as large as possible, where \(|\) denotes the bitwise OR.
Find the maximum possible value of \(a_1 | a_2 | ... | a_n\) after performing at most \(k\) operations optimally.
Input
The first line contains three integers \(n\), \(k\) and \(x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8)\).
The second line contains \(n\) integers \(a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9)\).
Output
Output the maximum value of a bitwise OR of sequence elements after performing operations.
Examples
input
3 1 2
1 1 1
output
3
input
4 2 3
1 2 4 8
output
79
Note
For the first sample, any possible choice of doing one operation will result the same three numbers \(1, 1, 2\) so the result is \(1 | 1 | 2 = 3\).
For the second sample if we multiply \(8\) by \(3\) two times we'll get \(72\). In this case the numbers will become \(1, 2, 4, 72\) so the OR value will be \(79\) and is the largest possible result.
Solution
题意
给定 \(n\) 个数 \(a_1 ... a_n\),可以进行 \(k\) 次操作,每次可以给任意一个数乘上 \(x\),求 \(a_1 | a_2 | ... | a_n\) 最大为多少。
题解
贪心 前缀和
因为 \(x \ge 2\),因此一个数乘上 \(x\) 后二进制位数必然增加一位。
由于或运算是 \(0 | 1\) 时才会使答案增加,因此让 \(k\) 个 \(x\) 乘在同一个数上就行。
计算一下前缀和和后缀和,然后暴力枚举每一个数乘以 \(x^k\),找到最大值即可。
Code
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
typedef long long ll;
ll a[maxn], s1[maxn], s2[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n, k, x;
cin >> n >> k >> x;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
s1[i] = s1[i - 1] | a[i];
}
for(int i = n; i >= 1; --i) {
s2[i] = s2[i + 1] | a[i];
}
ll ans = 0;
ll tmp = x;
for(int i = 2; i <= k; ++i) {
tmp *= x;
}
for(int i = 1; i <= n; ++i) {
ans = max(ans, s1[i - 1] | tmp * a[i] | s2[i + 1]);
}
cout << ans << endl;
return 0;
}
Codeforces 578B "Or" Game (前缀和 + 贪心)的更多相关文章
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- leetcode1546题解【前缀和+贪心】
leetcode1546.和为目标值的最大数目不重叠非空子数组数目 题目链接 算法 前缀和+贪心 时间复杂度O(n). 1.对nums数组求前缀和: 2.在求前缀和过程中将前缀和sum插入到set集合 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- Codeforces Testing Round #12 B. Restaurant 贪心
B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem ...
- Codeforces 437D The Child and Zoo(贪心+并查集)
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...
- CodeForces - 777B Game of Credit Cards 贪心
题目链接: http://codeforces.com/problemset/problem/777/B 题目大意: A, B玩游戏,每人一串数字,数字不大于1000,要求每人从第一位开始报出数字,并 ...
- Codeforces 1082C Multi-Subject Competition 前缀和 A
Codeforces 1082C Multi-Subject Competition https://vjudge.net/problem/CodeForces-1082C 题目: A multi-s ...
随机推荐
- 测开之路四十一:常用的jquery函数
jQuery选择器菜鸟教程:https://www.runoob.com/jquery/jquery-selectors.html 引用jquery2.1.1标签:<script src=&qu ...
- HDU 1205 吃糖果 (鸽巢原理)
题目链接:HDU 1205 Problem Description HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢 ...
- JAVA中的面向对象与内存解析_1
对象的创建和引用 必须使用new关键字创建对象. 使用对象(引用成员变量或来引用对象的成员变量. 使用对象(引用)方法(参数列表)来调用对象的方法. 同一类的每个对象有不同的成员变量存储空间. 同 ...
- CSS3 3D旋转下拉菜单--兼容性不太好
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Python - zipfile 乱码问题解决
最近使用zipfile进行解包过程中遇到了很不舒服的问题,解包之后文件名是乱码的.下面进行简单总结: 首先,乱码肯定是因为解码方式不一样了,zipfile使用的是utf-8和cp437这两种编码方式, ...
- Hibernate4教程五:事务和并发
Hibernate本身没有事务的实现 Hibernate 直接使用 JDBC 连接和 JTA 资源,不添加任何附加锁定行为.也就是说你在Hibernate里面使用的事务要么是JDBC的事务,要么是JT ...
- 【学习总结】Python-3-逻辑运算符
参考:菜鸟教程-Python3运算符 逻辑运算符的计算规则划重点: 并不是只返回布尔型,有时会返回变量的数值 (优先级:not>and>or) 总结: '与或非'三件套中,not与数学逻辑 ...
- JS面向对象——组合使用构造函数模型与原型模型中的隐患
组合使用构造函数模型和原型模型中的问题,使用对象字面量重写原型模型会有隐患(涉及到原型的动态性),如下例: <!DOCTYPE html> <html> <head> ...
- 从前端角度出发有哪些注意事项有利于SEO?
1.提高页面加载速度. 能用css解决的不用背景图片,背景图片也尽量压缩大小,可以几个icons放在一个图片上,使用background-position找到需要的图片位置.可以减少HTTP请求数,提 ...
- ubuntu下安装chrome谷歌浏览器
百度“chrome”然后登录谷歌浏览器官网下载deb包 cd到下载的目录下 sudo dpkg -i google-chrome*; 提示缺少依赖包,打入如下命令 sudo apt-get -f in ...