1. 程式人生 > >hdu5536 Chip Factory 【01字典樹刪減】

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  2  3  1 2 3  3  100 200 300

Sample Output  6  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;
}