标签:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 typedef long long ll; 7 /* 8 n个数,你最多有k次操作,每次操作可以选择一个数乘以x,问所有数或(|)的最大值 9 贪心思路:选一个数进行k此乘以x操作; 因为x>=2 10 111 ---> 1111 11 111 ---> 1111 12 不如 13 111 ---> 11111; 14 111 ---> 111; 15 用到了前缀和后缀的思想 : 16 */ 17 ll pre[200005]; // pre[i]是 a[1]|a[2].....|a[i]; 18 ll suf[200005]; 19 ll a[200005]; 20 ll ans; 21 int main(){ 22 23 int n,k,x; 24 while(scanf("%d%d%d",&n,&k,&x) != EOF){ 25 for(int i = 1 ; i <= n ; i ++) scanf("%I64d",&a[i]); 26 for(int i = 1 ; i <= n ; i ++) pre[i] = pre[i-1]|a[i]; 27 for(int i = n ; i >= 1 ; i --) suf[i] = suf[i+1]|a[i]; 28 ll num = 1; 29 for(int i = 1 ; i <= k ; i ++) num *= x; 30 for(int i = 1 ; i <= n ; i ++) ans = max(ans,pre[i-1]|a[i]*num|suf[i+1]); 31 printf("%I64d\n",ans); 32 } 33 34 35 return 0; 36 }
Codeforces Round #320 (Div. 2) "Or" Game(好题,贪心/位运算/前缀后缀或)
标签:
原文地址:http://www.cnblogs.com/zstu-jack/p/5396959.html