HDU 5536/ 2015长春区域 J.Chip Factory Trie

Chip Factory

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:
maxi,j,k(si+sj)sk

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.

1T1000
3n1000
0si109
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个数,为你不同的三个下标i,j,k,其中两个之和异或第三个的最大值是多少
题解:我们枚举其中两个得和,去不同下标最大,显然就裸字典树了。
///meek

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;


typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';ch=getchar();
    }return x*f;
}
//****************************************
const int N=8005;
#define mod 10000007

#define inf 10000007
#define maxn 10000

struct Trie{
    int ch[N*8][2],siz,sum[N],word[N*8];
    void init(){mem(ch),siz=1;mem(word);}
    void insertt(int x) {
         int u=0,k=1,W=x,each[33];mem(each);
         while(x) each[k++]=x%2,x/=2;
         for(int i=32;i>=1;i--) {
            int c=each[i];
            if(!ch[u][c]) {
                ch[u][c]=siz++;
                word[ch[u][c]]++;
            }
            else {
                word[ch[u][c]]++;
            }
            u=ch[u][c];
            if(i==1)sum[u]=W;
         }
    }
    int ask(int x) {
        int u=0,each[33],k=1,WW=x,g;mem(each);
        while(x) each[k++]=x%2,x/=2;
        for(int i=32;i>=1;i--) {
            int c=each[i];if(c==1)g=0;else g=1;
            if(ch[u][g]&&word[ch[u][g]]) {
                u=ch[u][g];
            }
            else u=ch[u][c];
            if(i==1) return WW^sum[u];
        }
    }
    void dele(int x) {
         int u=0,each[33],k=1,g;mem(each);
        while(x) each[k++]=x%2,x/=2;
        for(int i=32;i>=1;i--) {
            int c=each[i];
            u=ch[u][c];
            word[u]--;
        }
    }
}trie;

int main(){
    int n,a[N],T=read();
    while(T--) {
        scanf("%d",&n);trie.init();
        for(int i=1;i<=n;i++) {
            scanf("%d",&a[i]);
            trie.insertt(a[i]);
        }int ans=0;
        for(int i=1;i<=n;i++) {
            trie.dele(a[i]);
            for(int j=i+1;j<=n;j++) {
                trie.dele(a[j]);
                ans=max(ans,trie.ask(a[i]+a[j]));
                trie.insertt(a[j]);
            }
            trie.insertt(a[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}
代码

 

posted @ 2015-11-19 14:16  meekyan  阅读(301)  评论(0编辑  收藏  举报