CF578B “Or“ Game(思维+贪心+前缀或)

https://codeforces.com/problemset/problem/578/B


思路:

每次乘2在二进制里意味着该数至少能左移一位,对应高位贪心,我们把k次都集中到一个数上,然后考虑怎么求得其中一个数字改后的整个序列的答案。或满足前缀性质,二进制|不会变少。于是前缀或后缀或就好

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn],pre[maxn],suf[maxn];
LL ksm(LL a,LL k){
   LL res=1;
   while(k>0){
     if(k&1) res=res*a;
     a=a*a;
     k>>=1;
   }
   return res;
}
int main(void)
{
  LL n,k,x;n=read();k=read();x=read();
  for(LL i=1;i<=n;i++) a[i]=read();
  for(LL i=1;i<=n;i++) pre[i]=pre[i-1]|a[i];
  for(LL i=n;i>=1;i--) suf[i]=suf[i+1]|a[i];
  LL ans=0;
  for(LL i=1;i<=n;i++){
      LL temp=ksm(x,k);
      LL res=temp*a[i];
      ans=max(ans,pre[i-1]|res|suf[i+1]);
  }
  cout<<ans<<"\n";
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/115188510