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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- getEl mask 用法
- 第一章 初识MVC4
1.MVC模式 Mvc将应用程序分离为三个部分: Model:是一组类,用来描述被处理的数据,同时也定义这些数据如何被变更和操作的业务规则.与数据访问层非常类似. View:是一种动态生成HTML的模 ...
- wooyun本地数据抓取
---- #-*-coding:utf-8-*- import re import urllib import MySQLdb import time from urllib import unquo ...
- 小程序基础02:全局配置app.json
1.配置 我们使用app.json文件来对来微信小程序进行全局配置. 作用:他决定了页面文件的路径,窗口表现,设置网络超时时间,设置多tab等 每一个小程序页面也可以使用 .json 文件来对本页面的 ...
- JS 之继承
ECMAScript继承是通过原型链来继承的.基本思想是利用原型来让一个引用类型继承另一个引用类型的属性和方法,使原型变为另一个对象的实例.通过原型链实现继承时,不能使用对象字面量创建原型方法,避免重 ...
- Android中对JSONArray数组的指定项进行删除,更新。
首先假设有这么一个JSONArray JSONArray Array1;JSONArray ITEM = new JSONArray(); name为你获取要删除的字段名称,IETM就是你删除后得到的 ...
- Maven in 5 Minutes(Windows)
这是根据官网的例子写的入门例子:http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html 1)下载maven: ...
- 学习笔记——Maven实战(四)基于Maven的持续集成实践
Martin的<持续集成> 相信很多读者和我一样,最早接触到持续集成的概念是来自Martin的著名文章<持续集成>,该文最早发布于2000年9月,之后在2006年进行了一次修订 ...
- Lisp简明教程
此教程是我花了一点时间和功夫整理出来的,希望能够帮到喜欢Lisp(Common Lisp)的朋友们.本人排版很烂还望多多海涵! <Lisp简明教程>PDF格式下载 <Lisp简明教程 ...
- 大数据:从开源告诉你身边的IT故事
最近我们Team利用Dream分布式计算平台,做了这样一件事情,将Github的大量数据通过爬虫抓取下来,通过分析后,我们抽取最近一年中部分的开发者和项目信息,得到了如下有趣的信息,故分享之,数据原汁 ...