Loading

HDU5536 Chip Factory(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 ithi−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: max(i,j,k)(si+sj)skmax(i,j,k)(si+sj)⊕sk

which i,j,ki,j,k are three different integers between 1 and n. And ⊕ is symbol of bitwise XORXOR.

Can you help John calculate the checksum number of today?

输入

The first line of input contains an integerTT 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 n integers s1,s2,..,sns1,s2,..,sn separated with single space, indicating serial number of each chip.

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

输出

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

样例

输入

2
3
1 2 3
3
100 200 300

输出

6
400

Note

2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)

思路

  • 01字典树模板题
  • 暴力求解

代码

  1 /*
  2  * =========================================================================
  3  *
  4  *       Filename:  hdu5536.cpp
  5  *
  6  *           Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5536
  7  *
  8  *        Version:  1.0
  9  *        Created:  2018/08/30 20时46分09秒
 10  *       Revision:  none
 11  *       Compiler:  g++
 12  *
 13  *         Author:  杜宁元 (https://duny31030.top/), duny31030@126.com
 14  *   Organization:  QLU_浪在ACM
 15  *
 16  * =========================================================================
 17  */
 18 #include <bits/stdc++.h>
 19 using namespace std;
 20 #define clr(a, x) memset(a, x, sizeof(a))
 21 #define rep(i,a,n) for(int i=a;i<=n;i++)
 22 #define pre(i,a,n) for(int i=a;i>=n;i--)
 23 #define ll long long
 24 #define max3(a,b,c) fmax(a,fmax(b,c))
 25 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 26 const double eps = 1e-6;
 27 const int INF = 0x3f3f3f3f;
 28 const int mod = 1e9 + 7;
 29 const int N = 10100;
 30 int T,n,a[N];
 31 struct node
 32 {
 33     int next[2];
 34     int size;
 35 }trie[32*N];
 36 
 37 int tot = 1,root = 1;
 38 
 39 void Init()
 40 {
 41     rep(i,1,tot)
 42     {
 43         trie[i].next[0] = 0;
 44         trie[i].next[1] = 0;
 45         trie[i].size = 0;
 46     }
 47     tot = 1;
 48 }
 49 
 50 void Insert(int x)
 51 {
 52     int o = root;
 53     trie[o].size++;
 54     for(int k = 30;k >= 0;k--)
 55     {
 56         int tmp = 0;
 57         if(x&(1<<k))
 58             tmp = 1;
 59         if(!trie[o].next[tmp])
 60             trie[o].next[tmp] = ++tot;
 61 
 62         o = trie[o].next[tmp];
 63         trie[o].size++;
 64     }
 65 }
 66 
 67 void Delete(int x)
 68 {
 69     int o = root;
 70     trie[o].size--;
 71     for(int k = 30;k >= 0;k--)
 72     {
 73         int tmp = 0;
 74         if(x&(1<<k))
 75             tmp = 1;
 76         o = trie[o].next[tmp];
 77         trie[o].size--;
 78     }
 79 }
 80 
 81 int Query(int x)
 82 {
 83     int o = root;
 84     for(int k = 30;k >= 0;k--)
 85     {
 86         int tmp = 0;
 87         if(x&(1<<k))
 88             tmp = 1;
 89         if(tmp)
 90         {
 91             if(trie[o].next[0] && trie[trie[o].next[0]].size)
 92                 o = trie[o].next[0];
 93             else 
 94                 o = trie[o].next[1], x^=(1<<k);
 95         }
 96         else 
 97         {
 98             if(trie[o].next[1] && trie[trie[o].next[1]].size)
 99                 o = trie[o].next[1],x^=(1<<k);
100             else 
101                 o = trie[o].next[0];
102         }
103     }
104     return x;
105 }
106 int main()
107 {
108     ios
109 #ifdef ONLINE_JUDGE 
110 #else 
111         freopen("in.txt","r",stdin);
112     // freopen("out.txt","w",stdout); 
113 #endif
114     scanf("%d",&T);
115     while(T--)
116     {
117         int maxn = -INF;
118         scanf("%d",&n);
119         rep(i,1,n)
120         {
121             scanf("%d",&a[i]);
122             Insert(a[i]);
123         }
124         rep(i,1,n-1)
125         {
126             Delete(a[i]);
127             rep(j,i+1,n)
128             {
129                 Delete(a[j]);
130                 maxn = max(maxn,Query(a[i]+a[j]));
131                 Insert(a[j]);
132             }
133             Insert(a[i]);
134         }
135         printf("%d\n",maxn);
136         Init();
137     }
138     fclose(stdin);
139     // fclose(stdout);
140     return 0;
141 }

 

posted @ 2021-01-20 18:14  Yiduuannng  阅读(52)  评论(0)    收藏  举报