Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game
题目链接:http://codeforces.com/contest/578/problem/B
题目大意:现在有n个数,你可以对其进行k此操作,每次操作可以选择其中的任意一个数对其进行乘以x的操作。
思路:最先想到的办法是贪心,也就是选择数组中最大的那个数对它进行k次乘以x的操作。但是这种方法在有的时候会出现错误,如测试数据的第三组:
2 1 2
12 9
这种情况下有2个数,我能够对其进行1次乘以2的操作,如果以之前贪心的方法,选择12乘以2,那么得到的结果(12*2)|9=25,但是如果选择9乘以2,得到的结果是(9*2)|12=30。
考虑12和9的二进制:
12 = (1100)
9 = (1001)
12*2 =(11000)
9*2 =(10010)
12*2|9 =(11001)
9*2|12 =(11110)
可以看到,12*2因为和9在低位因为1|1的数量太多导致了出现12*2|9的结果小于9*2|12的结果,所以说这种贪心的方法是有不正确的地方的。
正确的方法是选择枚举每一个数a[i],计算a[i]*(x的k次方)与其余n-1个数的or和。
这种方法总能找到最优的答案。因为如果我不是选择一个数让他乘上所有的k个x,而是选择了大于1个数让他们分别乘上若干个x,那么在我最开始介绍的让最大的数乘以k个x的贪心方法肯定比这种方法得到更大的结果,因为这种贪心的方法得到的答案的二进制数的最高位一定比拆开来乘得到的二进制的方法得到更大的结果。
我用l[i]表示a[1]到a[i]的or sum,r[i]表示a[n]到a[i]的or sum,delta表示x的k次方,那么我只要枚举每一个l[i-1]|(a[i]*delta)|r[i]就可以了。
参考代码:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
const int maxn = ;
int n, k, x;
ll l[maxn], r[maxn], a[maxn], delta;
int main() {
cin >> n >> k >> x;
for(int i=;i<=n;i++) cin >> a[i];
for(int i=;i<=n;i++) l[i] = l[i-] | a[i];
for(int i=n;i>=;i--) r[i] = r[i+] | a[i];
delta = ; for(int i=;i<k;i++) delta *= x;
ll ans = ;
for(int i=;i<=n;i++) {
ll tmp = l[i-] | (a[i] * delta) | r[i+];
ans = ans > tmp ? ans : tmp;
}
cout << ans << endl;
return ;
}
Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game的更多相关文章
- 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 线段树贪心
B. "Or" Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/578 ...
- 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 ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E 三分+连续子序列的和的绝对值的最大值
E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C A Weakness and Poorness (三分)
显然f(x)是个凹函数,三分即可,计算方案的时候dp一下.eps取大了会挂精度,指定循环次数才是正解. #include<bits/stdc++.h> using namespace st ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B "Or" Game (贪心)
首先应该保证二进制最高位尽量高,而位数最高的数乘x以后位数任然是最高的,所以一定一个数是连续k次乘x. 当出现多个最高位的相同的数就枚举一下,先预处理一下前缀后缀即可. #include<bit ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] A A Problem about Polyline(数学)
题目中给出的函数具有周期性,总可以移动到第一个周期内,当然,a<b则无解. 假设移动后在上升的那段,则有a-2*x*n=b,注意限制条件x≥b,n是整数,则n≤(a-b)/(2*b).满足条件的 ...
随机推荐
- Spring_属性配置细节
XML 代码: <!-- 使用构造器注入属性值的位置和参数的类型!以区分重载的构造器! --> <bean id="car1" class="com.h ...
- PAT1063. Set Similarity (25)
来自http://blog.csdn.net/tiantangrenjian/article/details/16868399 set_intersection 交集 set_union 并集 s ...
- apache kafka配置中request.required.acks含义
Kafka producer的ack有3中机制,初始化producer时的producerconfig可以通过配置request.required.acks不同的值来实现. 0:这意味着生产者prod ...
- Python流程控制与函数
if >>> x = int(raw_input("Please enter an integer:")) Please enter an integer:42 ...
- define用于条件编译
格式: #ifndef _test.h_ //这里放不想被重复包含的代码 #define _test.h_ #endif define用于条件编译的意思是不想让头文件重复编译,头文件重复编译会造成的结 ...
- js 格式化时间日期函数小结3
function DateUtil(){}/***功能:格式化时间*示例:DateUtil.Format("yyyy/MM/dd","Thu Nov 9 20:30:37 ...
- 方便处理hosts的批处理脚本:hosts助手.bat
hosts助手.bat @echo off pushd "%~dp0" set sp0=------------------ set sp1=hosts助手 set hostsfi ...
- 教你10步闯进google play排行榜前列
1.正视最高榜单的价值 我们需要了解排名对你的游戏的价值,进入前20名你的游戏获得每日至少1万5千的安装量,而前10名获得至少2万5千的安装量.通过奖励性广告网络而获得这些流量需要你每日支付至 ...
- SVN提交出错--URL access forbidden for unknown reason
使用SVN在eclipse中提交文件,但是出现错误,如下: URL access forbidden for unknown reasonsvn: Commit failed (details fol ...
- New Concept English three (27)
35w/m 67 It has been said that everyone lives by selling something. In the light of this statement, ...