codeforces 579D D. "Or" Game(前后缀+贪心)
2016-06-09 23:35
381 查看
题目链接:
[b]D. "Or" Game[/b]
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
output
input
output
[b]D. "Or" Game[/b]
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
3 1 2 1 1 1
output
3
input
4 2 3 1 2 4 8
output
79 题意: 给一列数,任选一个数,乘x,最多操作k次,问最后a[1]|a[2]|...|a 的最大值是多少; 思路: 或运算是0|0=0,1|0=1,0|1=1,1|1=1,那么每次乘一个大于等于2的数就能使最高位数增加,那么肯定是把k个x都乘在一个数上才能最大,把a[1]|...|a 的前后缀都找出来,暴力枚举要找的那个数,得到最大值就好了,我以前连这么水的题都不会,想想好伤心; AC代码:
//#include <bits/stdc++.h> #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e9+7; const double PI=acos(-1.0); const LL inf=1e14; const int N=2e5+15; int n,k,x; LL a ,pre ,nex ,temp; LL solve(int x) { LL ans=a[x]*temp; return ans|pre[x-1]|nex[x+1]; } int main() { read(n);read(k);read(x); Riep(n)read(a[i]),pre[i]=(pre[i-1]|a[i]); for(int i=n;i>0;i--) { nex[i]=(nex[i+1]|a[i]); } temp=1; while(k--)temp=temp*x; LL ans=0; for(int i=1;i<=n;i++) ans=max(ans,solve(i)); cout<<ans<<"\n"; return 0; }
相关文章推荐
- View中的getScrollX、getScrollY与getLeft、getRight 以及 MotionEvent中的getX、getY与getRawX、getRawY
- CDN原理简介
- 一个将文件复制后以时间命名的Python程序
- 浅谈C++中指针和引用的区别
- MIT 6.828 学习笔记6 Lab4实验报告
- 使用ImageLoader实现图片异步加载
- Android 图片弹跳动画2
- 【Android内存泄漏】关于内存泄漏(三)
- 转发和重定向
- nodejs Controller层 封装
- 第二期冲刺站立会议个人博客16(2016/6/09)
- The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support.
- JAVA正则表达式匹配,替换,查找,切割
- [解决]UserLibrary中的jar包不会自动发布到Tomcat的lib目录下,而出现的tomcat开启服务报错问题
- 读《如何阅读一本书》乱摘
- MyEclipse无法自动编译
- zabbix原理
- 粗浅看 Tomcat系统架构分析
- Ubuntu64位系统下安装吉林大学校园网客户端
- [Linux]生产者与消费者 三种模型 C