hdu5536 Chip Factory 【01字典树删减】

Problem Description 
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 n chips today, the i-th chip produced this day has a serial number si.

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: 
 
which i,j,k are three different integers between 1 and n. 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 T indicating the total number of test cases.

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

1≤T≤1000 
3≤n≤1000 
0≤si≤109 
There are at most 10 testcases with n>100

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

Sample Input 


1 2 3 

100 200 300

Sample Output 

400

大致题意:给你n个数,让你从中选择三个不同的数,将其中两个数相加后异或上第三个数,使得结果最大,问最大结果为多少。

思路:先将这n个数建立成一颗01字典树,然后去枚举两个不同的数,将其从字典树中删除,然后再查询此时异或最大值,然后再向字典树中加入这两个数。
 

具体看代码:

#include<cstdio>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef __int64 ll;
const int N=2;
const int M=1005;

typedef struct Node
{
    int count;
    Node *Next[N];
} Node;

void change(ll t,ll a[])//转换为二进制
{
    for(ll i=0; i<32; i++)
        a[i]=(t>>(32-i-1))&1;
}

Node * build()//建立节点
{
    Node *node=(Node *)malloc(sizeof(Node));
    node->count=0;
    memset(node->Next,0,sizeof(node->Next));
    return node;
}

void tire_insert(Node *root,ll a[])//插入
{
    Node *p=root;
    p->count++;
    int i=0;
    while(i<32)
    {
        if(p->Next[a[i]]==NULL)
            p->Next[a[i]]=build();
        p=p->Next[a[i]];
        i++;
        p->count++;
    }
}
//删减和插入基本相似,就是  p->count++ 改为  p->count--

void tire_delate(Node *root,ll a[])//删减
{
    Node *p=root;
    p->count--;
    int i=0;
    while(i<32)
    {
        if(p->Next[a[i]]==NULL)
            return;
        p=p->Next[a[i]];
        i++;
        p->count--;
    }
}

ll found(Node *root,ll a[])//查找函数
{
    Node *p=root;
    ll i=0,ans=0;
    while(i<32)
    {
        if(p->Next[!a[i]]!=NULL&&p->Next[!a[i]]->count)
        {//这里表示  如果a[i]的不为空,就相当于两个数的这一位不相同,然后在
         //判断count是否为真   
            ans+=(1<<(32-i-1));
            p=p->Next[!a[i]];
        }
        else
            p=p->Next[a[i]];
        i++;
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int j,n,i;
        Node *root=build();
        ll a[32],tmp[M],ans=0;
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            scanf("%I64d",&tmp[i]);
            change(tmp[i],a);
            tire_insert(root,a);
        }
        for(i=0; i<n; i++)
        {
            change(tmp[i],a);//注意枚举的每个数要先转换成二进制
            tire_delate(root,a);
            for(j=i+1; j<n; j++)
            {
                change(tmp[j],a);//同上
                tire_delate(root,a);
                int t=tmp[i]+tmp[j];
                change(t,a);
                ans=max(ans,found(root,a));
                change(tmp[j],a);
                tire_insert(root,a);//并且这一步操作完记得再插入
            }
            change(tmp[i],a);
            tire_insert(root,a);//同上
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值