Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] -- D. "Or" Game
2015-09-17 13:34
627 查看
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.
题意:一组数共有k次机会对某个ai乘上x,问或值最大是多少
思路:或运算两个基本性质 a|b>=max(a,b) a|0=a,对于两个数a,b,假设b比a大,显然a|2b>=2a|b,所以显然k次操作应该作用在一个数上,不然平均下来所起的影响远不如在一个数上大,假设乘以的数为2,那么相当于进行一次左移,造成二进制数的加长,大于2就更明显了,所以只要对于1的所在位置高的数进行枚举,预处理前缀后缀,更新最大值。
比如2=10B 最高位1在第2位,5=101B 最高位1在第三位,因此只要枚举5即可。
其实要是嫌麻烦直接全部枚举一下就好了。
代码
Find the maximum possible value of after performing at most k operations optimally.
题意:一组数共有k次机会对某个ai乘上x,问或值最大是多少
思路:或运算两个基本性质 a|b>=max(a,b) a|0=a,对于两个数a,b,假设b比a大,显然a|2b>=2a|b,所以显然k次操作应该作用在一个数上,不然平均下来所起的影响远不如在一个数上大,假设乘以的数为2,那么相当于进行一次左移,造成二进制数的加长,大于2就更明显了,所以只要对于1的所在位置高的数进行枚举,预处理前缀后缀,更新最大值。
比如2=10B 最高位1在第2位,5=101B 最高位1在第三位,因此只要枚举5即可。
其实要是嫌麻烦直接全部枚举一下就好了。
代码
//枚举最高位 #include <bits/stdc++.h> #define LL long long using namespace std; LL n,k,x,a[200005]; LL p[200005],q[200005],u[200005]; LL fac(LL a) { LL ans=a; for(int i=0;i<k;++i)ans*=x; return ans; } vector <LL> v; int main() { cin>>n>>k>>x; LL maxx=-1; for(int i=1;i<=n;++i)cin>>a[i],p[i]=p[i-1]|a[i]; for(int i=n;i>=1;--i)q[i]=q[i+1]|a[i]; for(int i=1;i<=n;++i) { LL r=1,l=30;r<<=30; while(a[i]>=r)r>>=1,--l; u[i]=l;maxx=max(maxx,l); } for(int i=1;i<=n;++i)if(u[i]==maxx)v.push_back(i); int len=v.size(); maxx=-1; for(int i=0;i<len;++i) { maxx=max(fac(a[v[i]])|p[v[i]-1]|q[v[i]+1],maxx); } cout<<maxx<<endl; return 0; }
//直接枚举 #include <bits/stdc++.h> #define LL long long using namespace std; LL n,k,x,a[200005]; LL p[200005],q[200005]; LL fac(LL a) { LL ans=a; for(int i=0;i<k;++i)ans*=x; return ans; } int main() { cin>>n>>k>>x; LL maxx=-1; for(int i=1;i<=n;++i)cin>>a[i],p[i]=p[i-1]|a[i]; for(int i=n;i>=1;--i)q[i]=q[i+1]|a[i]; for(int i=1;i<=n;++i) { maxx=max(fac(a[i])|p[i-1]|q[i+1],maxx); } cout<<maxx<<endl; return 0; }
相关文章推荐
- Search and replace string
- AIX加入能telnet远程连接方法的帐户
- Linux中wait用法注意
- java基础回顾与进阶
- leetcode/Next Permutation
- 我的openwrt学习笔记(十三):MT7688 串口测试
- OpenLayers学习--加载ArcGIS缓存
- Javascript 数组循环遍历之forEach
- redhat下升级gcc编译器
- SSH中使用getCurrentSession()获得session
- iOS9搜索新功能将狙击谷歌的搜索业务
- 【中国剩余定理-M不互质的情况】HDU Hello Kiki 2579
- 【Android】轮询的若干问题 Timer, TimerTask;Timer 的缺陷,ScheduledThreadPoolExecutor
- 创建兼容的DC
- 多态
- 302机房远程电脑地址
- iOS中Bitcode的介绍及配置
- 内核树的建立
- 异常的中英文对照
- 关于运行时无法找到符号的问题--笔试题