Chip Factory (HDU-5536)(01字典树)

John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage large amounts of products, every processor has a serial number. More specifically, the factory produces nn chips today, the ii-th chip produced this day has a serial number sisi.

At the end of the day, he packages all the chips produced this day, and send it to wholesalers. More specially, he writes a checksum number on the package, this checksum is defined as below:

maxi,j,k(si+sj)⊕skmaxi,j,k(si+sj)⊕sk


which i,j,ki,j,k are three different integers between 11 and nn. And ⊕⊕ is symbol of bitwise XOR.

Can you help John calculate the checksum number of today?

Input

The first line of input contains an integer TT indicating the total number of test cases.

The first line of each test case is an integer nn, indicating the number of chips produced today. The next line has nn integers s1,s2,..,sns1,s2,..,sn, separated with single space, indicating serial number of each chip.

1≤T≤10001≤T≤1000
3≤n≤10003≤n≤1000
0≤si≤1090≤si≤109
There are at most 1010 testcases with n>100n>100

Output

For each test case, please output an integer indicating the checksum number in a line.

Sample Input

2
3
1 2 3
3
100 200 300

Sample Output

6
400

 

题意:给你n个数,让你在n个数中选三个,使得(ai+aj)^ak的值最大,且ai!=aj!=ak。

思路:这道题的话,因为考虑到是求异或的最大值,所以我们可以用01字典树的方法来写,先把所有的数加入字典树中,然后从n个数中选出两个数a[i]和a[j],先把他们从字典树中删除,然后找到与a[i]+a[j]异或最大的数,和结果取最大值。然后不要忘记再把a[i]和a[j]添加到字典树中。

AC代码:

#include <bits/stdc++.h>
typedef long long ll;
const int maxx=2010;
const int maxn=2;
const int inf=0x3f3f3f3f;
using namespace std;
int Trie[32*maxx][maxn];
ll color[32*maxx];
int a[maxx];
int num[32*maxx];
int k;
void init()
{
    memset(Trie,0,sizeof(Trie));
    memset(color,0,sizeof(color));
    memset(num,0,sizeof(num));
    k=0;
}
void insert(ll w)
{
    int p=0;
    for(int i=32; i>=0; i--)
    {
        int c=(w>>i)&1;
        if(!Trie[p][c])
        {
            Trie[p][c]=++k;
        }
        p=Trie[p][c];
        num[p]++;
    }
    color[p]=w;
}
void update(ll w,int cnt)
{
    int p=0;
    for(int i=32; i>=0; i--)
    {
        int c=(w>>i)&1;
        p=Trie[p][c];
        num[p]+=cnt;
    }
}
ll search(ll w)
{
    int p=0;
    for(int i=32; i>=0; i--)
    {
        int c=(w>>i)&1;
        if(c==1)
        {
            if(Trie[p][0] && num[Trie[p][0]])
                p=Trie[p][0];
            else
                p=Trie[p][1];
        }
        else
        {
            if(Trie[p][1] && num[Trie[p][1]])
                p=Trie[p][1];
            else
                p=Trie[p][0];
        }
    }
    return color[p];
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        ll ans=0;
        int n;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            insert(a[i]);
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=i+1; j<=n; j++)
            {
                update(a[i],-1);
                update(a[j],-1);
                ans=max(ans,(a[i]+a[j])^search(a[i]+a[j]));
                update(a[i],1);
                update(a[j],1);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值