Codeforces 578B "Or" Game
You are given $n$ numbers $a_1, a_2, \dots, 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\mid a_2\mid \dots\mid a_n$ as large as possible, where $\mid$ denotes the bitwise OR. Find the maximum possible value of $a_1\mid a_2\mid \dots\mid a_n$ after performing at most $k$ operations optimally.
Input
The first line contains three integers $n$, $ k $ and $ x $ ($1 \le n \le 200000$, $1 \le k \le 10$, $2 \le x \le 8$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$).
Output the maximum value of a bitwise OR of sequence elements after performing operations.
3 1 2
1 1 1
3
4 2 3
1 2 4 8
79
For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is
.
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
注意到 $2\le x\le 8$,意味着:每次不论选那个数乘以 $x$,这个数的二进制位数都会增加。
(其实这不算什么Key Observation,当 $x>1$ 时,$x$ 乘任何正整数,该数的二进制位数都会增加)
所以要取得最大值,$k$ 次必然都是乘以同一个数,结果的位数就是这个数最后的位数
所以算法是:
将输入数组从大到小排序,枚举乘 $x^k$ 后长度最长的数,更新答案。
为此,预处理出输入数组前缀和后缀取或(|)的结果。
Implementation
#include <bits/stdc++.h>
using namespace std;
const int N(2e5+);
typedef long long ll;
typedef pair<int,int> P;
P a[N];
int f[N], b[N];
int high_bit(ll x){
for(int i=; i>=; i--){
if(x&(ll)<<i) return i;
}
}
int main(){
int n, k, s;
scanf("%d%d%d", &n, &k, &s);
for(int i=, x; i<=n; i++) scanf("%d", &x), a[i]={x, i}; f[]=;
for(int i=; i<=n; i++) f[i]=f[i-]|a[i].first;
b[n+]=;
for(int i=n; i; i--) b[i]=b[i+]|a[i].first; sort(a+, a+n+, greater<P>());
if(!a[].first){puts(""); return ;}
ll _=; for(int i=; i<k; i++) _*=s; ll ans=, lar=_*a[].first;
int id, h=high_bit(lar);
for(int i=; i<=n; i++){
lar=_*a[i].first;
if(high_bit(lar)<h) break;
id=a[i].second;
ans=max(ans, f[id-]|b[id+]|lar);
}
printf("%lld\n", ans);
}
比赛时把
typedef long long ll;
写成了
typedef long ll;
结果交上去连样例都没过,但在自己电脑上却没问题, 这是由于 long / long long 的具体长度依赖于机器 (machine-dependent),
C++ 标准只规定了其最小长度(minimum size),long(32 bits)long long(64 bits).
除去这个细节不谈,比赛时还犯了个算法上的错误:
枚举原来最长的数而不是乘 $x^k$ 后最长的数。
UPD 2018/3/18
难道不是用最大的数乘 $x^k$ 吗?
Codeforces 578B "Or" Game的更多相关文章
- Codeforces 578B "Or" Game (前缀和 + 贪心)
Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] 题目链接:B. "Or" Game You are given \(n\) ...
- codeforces 578b//"Or" Game// Codeforces Round #320 (Div. 1)
题意:n个数字,最多操作k次,每次乘x,要使结果数组的与值最大. 能推断出结果是对一个元素操作k次,并且这个元素的二进制最高位比较大.并不一定是取最大的,比如1100和1010,乘以一次2,两种选法分 ...
- Codeforces 578B. "Or" Game(思维题)
我们知道所有sigma(2^i){i<n}比2^n小,所以我们肯定是把这k次操作全部丢到一个数上看看能不能凑出二进制下一个更高位的1. 因为k最大只有10,我们可以求出每一个数乘以k次之后的值, ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- Mac下Android Studio中获取SHA1和MD5
有很多人讲这个的时候,老是只把这个代码标出来又不说为什么 keytool -list -keystore debug.keystore keytool 这个是java的 jdk中一个工具(做签名文 ...
- 3016 质子撞击炮 II
3016 质子撞击炮 II 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description [抱歉数据错误~~已修 ...
- [vm]exsi的名词
早上想了想高可用.昨晚想学学虚拟vmware的一些东西. 物理机---exsi---vm 物理机-win7-vmware-vm 服务器高可用: 一 名词 1,虚拟网络 2,虚拟交换机 3,vm ...
- Python基础 - 获取N天前的日期
获取今天前第N天的日期 from datetime import datetime from datetime import timedelta def get_date(days=N): retur ...
- System类及其常用函数
System 类包含一些有用的类字段和方法.它不能被实例化. 常用方法: 1.static void arraycopy(Object src, int srcPos, Object dest, in ...
- Java集合---Arrays类源码解析
一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Primitive(8种基本类型)和Object两大类. 基本类型:采用调优的快速排序: 对象类型: ...
- [CareerCup] 6.4 Blue Eyes People on Island 岛上的蓝眼人
6.4 A bunch of people are living on an island, when a visitor comes with a strange order: all blue-e ...
- 你所不知道的Python奇技淫巧
有时候你会看到很Cool的Python代码,你惊讶于它的简洁,它的优雅,你不由自主地赞叹:竟然还能这样写.其实,这些优雅的代码都要归功于Python的特性,只要你能掌握这些Pythonic的技巧,你一 ...
- 【MPI学习2】MPI并行程序设计模式:对等模式 & 主从模式
这里的内容主要是都志辉老师<高性能计算之并行编程技术——MPI并行程序设计> 书上有一些代码是FORTAN的,我在学习的过程中,将其都转换成C的代码,便于统一记录. 这章内容分为两个部分: ...
- Android开发者资源大汇总
本文总结了最新的Android开发资源.下面列出的资源都是常用的,每个Android程序员都应该知道,能大大方便App开发.Enjoy~ 来源:Android开发周刊 中文的Android开发信息,资 ...