CF578B "Or" Game (thinking + greedy + prefix or)

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


Ideas:

Each time multiplying by 2 in binary means that the number can be shifted by at least one bit to the left, corresponding to high greed. We concentrate k times on one number, and then consider how to obtain the answer to the entire sequence after one of the numbers is modified. Or to satisfy the prefix property, the binary | will not decrease. So prefix or suffix or just fine

#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;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/115188510
Recommended