codeforces 579D D. "Or" Game(前后缀+贪心)

题目链接:

D. "Or" Game

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

 
Input
 

The first line contains three integers nk and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8).

The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).

 
Output
 

Output the maximum value of a bitwise OR of sequence elements after performing operations.

 
Examples
 
input
3 1 2
1 1 1
output
3
input
4 2 3
1 2 4 8
output
79

题意:

给一列数,任选一个数,乘x,最多操作k次,问最后a[1]|a[2]|...|a[n]的最大值是多少;

思路:

或运算是0|0=0,1|0=1,0|1=1,1|1=1,那么每次乘一个大于等于2的数就能使最高位数增加,那么肯定是把k个x都乘在一个数上才能最大,把a[1]|...|a[n]的前后缀都找出来,暴力枚举要找的那个数,得到最大值就好了,我以前连这么水的题都不会,想想好伤心;

AC代码:

//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e14;
const int N=2e5+15;

int n,k,x;
LL a[N],pre[N],nex[N],temp;
LL solve(int x)
{
    LL ans=a[x]*temp;
    return ans|pre[x-1]|nex[x+1];
}

int main()
{
    read(n);read(k);read(x);
    Riep(n)read(a[i]),pre[i]=(pre[i-1]|a[i]);
    for(int i=n;i>0;i--)
    {
        nex[i]=(nex[i+1]|a[i]);
    }
    temp=1;
    while(k--)temp=temp*x;
    LL ans=0;
    for(int i=1;i<=n;i++)
        ans=max(ans,solve(i));
    cout<<ans<<"
";
        return 0;
}
原文地址:https://www.cnblogs.com/zhangchengc919/p/5572966.html