[JZOJ4828]最大值

9 篇文章 0 订阅
7 篇文章 0 订阅

题目描述

给定一个含有 n 个正整数的序列A,以及一个位运算操作符 op ,请求出所有 Ai op Aj(1i<jn) 中的结果的最大值。
其中 op 可能是与、或和异或。
本题数据 T 组数据。

1T6,2n105,1Ai<220


题目分析

考虑枚举每一个 Ai ,然后二进制从高位到低位贪心得到最优解。
如果运算符是异或,显然我们可以在字典树上查找。
如果运算符是与或者或,我们在贪心的过程中相当于限制了二进制某些位置一定为 1 (既要满足当前的也要满足以前的约束)。我们现在要解决的问题是如何求出A中二进制某些位置一定为 1 的数的个数。
fx表示 A 中包含x这个二进制的数的个数(包含即 x 上有1的位其一定为 1 )。如果直接算是O(n max{Ai})的,肯定会T。考虑使用类似于[COCI2012]Toy/[JZOJ3170]挑选玩具一题的分治算法。我们先把所有 f(Ai) 1 ,然后从高位到低位递归。显然考虑到第i位时,一个数 x 如果被另外一个数y包含,要么是 y i这一位比 x 多了1,要么这一位相同在更高位不同。直接将右半部分的 f 加到左半部分即可。具体看链接,这里不详细讲。这样的时间复杂度是O(Alog2A)的。
总的时间复杂度 O(nlog2A+Alog2A)


代码实现

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>

using namespace std;

inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while (!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar();
    while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x*f;
}

const int N=100500;
const int D=20;
const int V=1<<D;

int n,c,T,ans;
int num[N];

inline int opt(int x,int y){return c==1?x&y:(c==2?x^y:x|y);}

namespace XOR
{
    const int S=N*(D+1);

    struct trie
    {
        int next[S][2],size[S];
        int tot,root;

        int newnode()
        {
            size[++tot]=0,next[tot][0]=next[tot][1]=0;
            return tot;
        }

        void init(){tot=0,root=newnode();}

        void insert(int x)
        {
            int rt=root;
            size[rt]++;
            for (int d=D-1;d>=0;d--)
            {
                if (!next[rt][(x>>d)&1]) next[rt][(x>>d)&1]=newnode();
                size[rt=next[rt][(x>>d)&1]]++;
            }
        }

        int maxxor(int x)
        {
            int rt=root,ret=0;
            for (int d=D-1;d>=0;d--) rt=next[rt][((x>>d)&1)^1]?ret+=1<<d,next[rt][((x>>d)&1)^1]:next[rt][(x>>d)&1];
            return ret;
        }
    }t;

    void calc()
    {
        ans=-1,t.init();
        for (int i=1;i<=n;i++)
        {
            if (i>1) ans=max(ans,t.maxxor(num[i]));
            t.insert(num[i]);
        }
    }
};

namespace ANDOR
{
    int f[V];

    void pre(int l,int r)
    {
        if (l==r) return;
        int mid=l+r>>1;
        pre(l,mid),pre(mid+1,r);
        for (int i=0;i<=mid-l;i++) f[l+i]+=f[mid+1+i];
    }

    void calc()
    {
        memset(f,0,sizeof f);
        for (int i=1;i<=n;i++) f[num[i]]++;
        pre(0,V-1);
        ans=-1;
        for (int i=1,cur,cst;i<=n;i++)
        {
            cur=cst=0;
            for (int d=D-1;d>=0;d--)
                if (c==3)
                    if (!((num[i]>>d)&1))
                    {
                        cst+=1<<d;
                        if (f[cst]) cur+=1<<d;
                        else cst-=1<<d;
                    }
                    else cur+=1<<d;
                else
                    if ((num[i]>>d)&1)
                    {
                        cst+=1<<d;
                        if (f[cst]>1) cur+=1<<d;
                        else cst-=1<<d;
                    }
            ans=max(ans,cur);
        }
    }
}

int main()
{
    freopen("maximum.in","r",stdin),freopen("maximum.out","w",stdout);
    for (T=read();T--;)
    {
        n=read(),c=read();
        for (int i=1;i<=n;i++) num[i]=read();
        if (c==2) XOR::calc();
        else ANDOR::calc();
        printf("%d\n",ans);
    }
    fclose(stdin),fclose(stdout);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值