您的位置:首页 > 其它

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即可。

其实要是嫌麻烦直接全部枚举一下就好了。

代码

//枚举最高位
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: