Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] -- D. "Or" Game

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值