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 ...
随机推荐
- java之内部类与匿名内部类
Java 内部类 分四种:成员内部类.局部内部类.静态内部类和匿名内部类. 1.成员内部类: 即作为外部类的一个成员存在,与外部类的属性.方法并列. 注意:成员内部类中不能定义静态变量,但可以访问外部 ...
- 错误代码: 1005 Can't create table 'hibernate.bill' (errno: 150)
主要问题以及解决办法是: 1,MySQL支持外键约束,并提供与其它DB相同的功能,但表(外键表和外键主表)类型必须为 InnoDB,外键表和外键主表的类型都要是innoDB 建表约束语句: user表 ...
- linux aio
前几天nginx的0.8.x正式成为stable,然后看了下代码,发现0.8加入了linux native aio的支持,我们知道在linux下有两种aio,一种是glibc实现的aio,这个比较烂, ...
- bzoj1150: [CTSC2007]数据备份Backup
题目大意: 在n个点中,选出k对相邻的互不相同的点,使k段距离的总和最小. 贪心,双向链表. 首先,点之间的距离是动态的,所以要用堆来维护. 每次都选择最近的点.但因为其他情况,可能最终不会选择这 ...
- 解决angular的post请求后SpringMVC后台接收不到参数值问题的方法
这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...
- jquery滚动条
查看demo: 下载Demo
- [面试题] for() while() 条件判断 赋值问题
http://group.jobbole.com/7963/#comm-11311 [题目]:下列for循环的循环体执行次数为 for(int i=10, j=1; i=j=0; i++, j--)( ...
- Spring 教程(二)
一.Spring AOP介绍 开发其实就是在不断的重构,抽象重复代码,然后进行封装.从最原始的模块化编程到面向对象编程,代码的封装越来越整齐清晰,但是依然存在重复的代码,而这些重复代码几乎都是与业务逻 ...
- 什么是MBeanServer
什么是MBeanServer MBeanServer是一个包含所有注册MBean的仓库.它是JMX代理层的核心.JMX1.0规范提供一个接口叫 javax.management.MBeanServer ...
- 《深入Java虚拟机学习笔记》- 第12章 整数运算
Java虚拟机提供几种进行整数算术运算的操作码,他们执行基于int和long类型的运算.当byte.short和char类型值参与算术运算时,首先会将它们转换为int类型.这些操作码都不会抛出异常,溢 ...