zoukankan      html  css  js  c++  java
  • Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心

    B. "Or" Game

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/578/problem/B

    Description

    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.

    Sample Input

    3 1 2
    1 1 1

    Sample Output

    3

    HINT

    题意

    给你n个数,你可以操作k次,使得其中的某一个数乘以x

    要求最后得到的所有数的或值最大

    题解:

    首先dp是错的,因为a>b不能保证a|c>b|c

    这儿有一个贪心的操作,如果又一次乘法给了a1,那么剩下的都得给a1,这样才能得到最优值

    因为x>=2可以保证最高位的1在不断增加

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 2000000 + 500
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    /*
    
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    */
    //**************************************************************************************
    long long n , k , x;
    
    struct tree
    {
        int L , R  ;
        ll sum;
    };
    
    tree a[maxn * 4];
    ll d[maxn];
    void build(int x,int l,int r)
    {
        a[x].L = l,a[x].R = r;
        if(l == r)
        {
            a[x].sum = d[l];
            return;
        }
        else
        {
            int mid = (l+r)>>1;
            build(x<<1,l,mid);
            build(x<<1|1,mid+1,r);
            a[x].sum = a[x<<1].sum | a[x<<1|1].sum;
        }
    }
    
    ll query(int o,int QL,int QR)
    {
        int L = a[o].L , R = a[o].R;
        if (QL <= L && R <= QR) return a[o].sum;
        else
        {
            int mid = (L+R)>>1;
            ll res = 0;
            if (QL <= mid) res |= query(2*o,QL,QR);
            if (QR > mid) res |= query(2*o+1,QL,QR);
            return res;
        }
    }
    int main()
    {
        cin>>n>>k>>x;
    
        for(int i = 1 ; i <= n ; ++ i)
        {
            scanf("%I64d",&d[i]);
        }
        build( 1 , 0 , n+1);
        ll ans = 0;
        ll temp = 1;
        for(int i=1;i<=k;i++)
            temp *= x;
        for(int i = 1 ; i <= n ; ++ i)
        {
            ans = max( ans , (d[i]*temp) | query(1,0,i-1) | query(1,i+1,n+1));
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    loj 1251(2-sat + 输出一组可行解)
    hdu 4751(dfs染色)
    hdu 2545(并查集求节点到根节点的距离)
    uva 10972(边双连通分量)
    uva 10246(最短路变形)
    uva 11380(最大流+拆点)
    hdu 4640(状压dp)
    hdu 1430+hdu 3567(预处理)
    python基础知识回顾[1]
    基于websocket搭建简易群聊
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4815081.html
Copyright © 2011-2022 走看看