Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心
B. "Or" Game
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/578/problem/B
Description
You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where
denotes the bitwise OR.
Find the maximum possible value of 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 a1, a2, ..., an (0 ≤ ai ≤ 109).
Output
Output the maximum value of a bitwise OR of sequence elements after performing operations.
Sample Input
3 1 2
1 1 1
Sample Output
3
HINT
题意
给你n个数,你可以操作k次,使得其中的某一个数乘以x
要求最后得到的所有数的或值最大
题解:
首先dp是错的,因为a>b不能保证a|c>b|c
这儿有一个贪心的操作,如果又一次乘法给了a1,那么剩下的都得给a1,这样才能得到最优值
因为x>=2可以保证最高位的1在不断增加
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000000 + 500
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
//**************************************************************************************
long long n , k , x; struct tree
{
int L , R ;
ll sum;
}; tree a[maxn * ];
ll d[maxn];
void build(int x,int l,int r)
{
a[x].L = l,a[x].R = r;
if(l == r)
{
a[x].sum = d[l];
return;
}
else
{
int mid = (l+r)>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
a[x].sum = a[x<<].sum | a[x<<|].sum;
}
} ll query(int o,int QL,int QR)
{
int L = a[o].L , R = a[o].R;
if (QL <= L && R <= QR) return a[o].sum;
else
{
int mid = (L+R)>>;
ll res = ;
if (QL <= mid) res |= query(*o,QL,QR);
if (QR > mid) res |= query(*o+,QL,QR);
return res;
}
}
int main()
{
cin>>n>>k>>x; for(int i = ; i <= n ; ++ i)
{
scanf("%I64d",&d[i]);
}
build( , , n+);
ll ans = ;
ll temp = ;
for(int i=;i<=k;i++)
temp *= x;
for(int i = ; i <= n ; ++ i)
{
ans = max( ans , (d[i]*temp) | query(,,i-) | query(,i+,n+));
}
cout<<ans<<endl;
}
Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心的更多相关文章
- Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树
题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...
- Codeforces Round #222 (Div. 1) B. Preparing for the Contest 二分+线段树
B. Preparing for the Contest 题目连接: http://codeforces.com/contest/377/problem/B Description Soon ther ...
- Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树
D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C. Weakness and Poorness 三分 dp
C. Weakness and Poorness Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game
题目链接:http://codeforces.com/contest/578/problem/B 题目大意:现在有n个数,你可以对其进行k此操作,每次操作可以选择其中的任意一个数对其进行乘以x的操作. ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E. Weakness and Poorness 三分
E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】
A. Raising Bacteria time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D 数学+(前缀 后缀 预处理)
D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- Tomcat详解
解压缩下载的Tomcat压缩包,呈现的目录结构如下. bin:目录存放一些启动和关闭Tomcat的可执行程序和相关内容.conf:存放关于Tomcat服务器的全局配置.lib:目录存放Tomcat运行 ...
- UNICODE,GBK,UTF-8区别
简单来说,unicode,gbk和大五码就是编码的值,而utf-8,uft-16之类就是这个值的表现形式.而前面那三种编码是一兼容的,同一个汉字,那三个码值是完全不一样的.如"汉"的uncode值与g ...
- Access增删改查 (持续更新中)
关于Access数据库(2003)的增删改查,其实和Sql大体差不多,但是还有很多不一样的地方.下面列几个容易犯的错误: 1.Access数据库的位置: conn = new OleDbConnec ...
- POJ 1273 (基础最大流) Drainage Ditches
虽然算法还没有理解透,但以及迫不及待地想要A道题了. 非常裸的最大流,试试lrj的模板练练手. #include <cstdio> #include <cstring> #in ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]PrI.6.1
Given a basis $U=(u_1,\cdots,u_n)$ not necessarily orthonormal, in $\scrH$, how would you compute th ...
- 利用文件实现Free Pascal中的简单排序功能
此程序主要是验证文件功能的读写功能,总结到的东西有:①文件无论是读还是写,都要先建立链接关系才可以进行;②读与写不能同时进行,必须分开操作,这也可以理解,在实际鼠标操作时也是如此的!③读写后必须用cl ...
- iOS 之NSJSONReadingOptions说明【转】
首先用代码来说明NSJSONReadingMutableContainers的作用: NSString *str = @"{\"name\":\"kaixuan ...
- HOG:从理论到OpenCV实践
(转载请注明出处:http://blog.csdn.net/zhazhiqiang/ 未经允许请勿用于商业用途) 一.理论 1.HOG特征描述子的定义: locally normalised ...
- delphi获取mdb密码
function GetPasswordFromAccess(AFileName: string): string;var myms: TMemoryStream; b: array of Byt ...
- 【DOM】学习笔记
三. 一份文档就是一颗节点树 节点类型:元素节点——属性节点.文本节点 getElementById()返回一个对象,对应一个元素节点 getElementByTagName()返回一个对象数组,分别 ...