【HDU

来源:互联网 发布:星球基地mac 编辑:程序博客网 时间:2024/06/04 19:34

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)⊕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是大于100的,所有我们枚举所有si+sj 的情况是允许的,关键是对于i j k 这三个序号不重复这一点怎么处理,虚删除操作,我们保留被删除的节点,但是标记其是没法走的路,删除i j 之后再重新插入,这时因为还保留当时的节点,就不用重新开空间了。
代码

#include<bits/stdc++.h>using namespace std;typedef pair<int,int>pii;#define first fi#define second se#define  LL long longconst int MAXN =1000+11;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;int ch[MAXN*32][2],sz;int num[MAXN*32];  // 记录每个节点的数目。 void init(){    memset(ch[0],0,sizeof(ch[0]));    sz=1;    memset(num,0,sizeof(num));}void Insert(LL a){    int u=0;    for(int i=31;i>=0;i--){        int c=1&(a>>i);        if(!ch[u][c]){            memset(ch[sz],0,sizeof(ch[sz]));            ch[u][c]=sz++;        }        u=ch[u][c];        num[u]++;    }}void Delete(LL a){    int u=0;    for(int i=31;i>=0;i--){        int c=1&(a>>i);        num[ch[u][c]]--;        u=ch[u][c];    }}LL Query(LL a){    int u=0; LL ans=0;    for(int i=31;i>=0;i--){        int c=1&(a>>i);        if(ch[u][c^1]&&num[ch[u][c^1]]) { //不仅要有这个节点,同时还要能够走这个节点             ans|=1<<i;       // 其实写成这样 ans|=(c^(c^1))<<i ; 对于入门的人可能更容易理解          //  puts("====");            u=ch[u][c^1];        }        else             u=ch[u][c];    }    return ans;}LL a[MAXN];int main(){    int T;scanf("%d",&T);    while(T--){        init();        int n;scanf("%d",&n);        for(int i=1;i<=n;i++){            scanf("%lld",&a[i]);            Insert(a[i]);        }        LL MAX =-1;         for(int i=1;i<=n;i++){            for(int j=i+1;j<=n;j++){                Delete(a[i]); Delete(a[j]);                //printf("a[i]  =%lld  a[j]==%lld  %lld\n",a[i],a[j],Query(a[i]+a[j]));                MAX=max(Query(a[i]+a[j]),MAX);                Insert(a[i]); Insert(a[j]);             }         }         printf("%lld\n",MAX);     }    return 0;}
原创粉丝点击