"Or" Game

原创 2016年08月28日 18:36:47
链接:http://codeforces.com/problemset/problem/579/D

题目:

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.



题意:给一串数,可以对任意数乘x,总共乘k次。为最后的全部异或和最大为多少

分析:基本是贪心,首先要求异或和最大,那么最好的情况是所有的k个x都乘到一起,因为这样数位最大,其次,保证低位的1尽可能的为奇数,但是却比较麻烦。查阅资料后发现一种另类的前缀和,这里可以叫做前缀异或和以及后缀异或和。输入后预处理一个前缀异或和和一个后缀异或和(均不包含自己),之后对每一个数求前缀和*自己*后缀和*k个x,比较得出最大值。


题解:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
#define rt return 0
#define fr freopen("in.txt","r",stdin)
#define fw freopen("out.txt","w",stdout)
#define ll long long
using namespace std;

int a[200010];
int pre[200010];
int old[200010];

int main()
{
	int n, k, x;
	cin >> n >> k >> x;
	for (int i = 1; i <= n;i++)
	{
		cin >> a[i];
		pre[i] = pre[i - 1] | a[i];
	}
	for (int i = n; i > 0; i--)
		old[i] = old[i + 1] | a[i];
	ll l = 1, ans = 0;
	for (int i = 0; i < k; i++)
		l *= (ll)x;
	for (int i = 1; i <= n;i++)
	{
		ans = max(ans, (l*(ll)a[i]) | (ll)(pre[i - 1]) | (ll)(old[i + 1]));
	}
	cout << ans << endl;
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

hdu1505 City Game(DP or 扫描线?)

City Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota...

How to work around a possible XNA Game Studio or Windows Phone SDK install failure on Windows 8

As announced earlier today on the Building Windows 8 blog, a Windows 8 consumer preview is avail...

“Or" Game CodeForces - 578B (暴力)

You are given n numbers a1, a2, …, an. You can perform at most k operations. For each operation you ...

【21.37%】【codeforces 579D】"Or" Game

time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandar...

【Codeforces 578B】【贪心】"Or" Game

“Or” Game Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u ...

HDU 2115 I Love This Game(结构体排序 or pair)

I Love This Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others...

UVa 2038 - Strategic game(二分图最小顶点覆盖 or 树形DP)

题意:给定一棵树,选择尽量少的点,使得每个没有选中的结点至少和一个已经选中的结点相邻。输出最少需要选择的节点数。 思路:经典的二分图最小顶点覆盖, 也是经典的树形 DP 。 最小顶点覆盖 == 最大匹...

Codeforces 579D "Or" Game【贪心+暴力按位处理】

D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input sta...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:"Or" Game
举报原因:
原因补充:

(最多只允许输入30个字)