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('\n');
}

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<<"\n";
        return 0;
}

 

转载于:https://www.cnblogs.com/zhangchengc919/p/5572966.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值