Chip Factory
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 8328 Accepted Submission(s): 3752
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.
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
题目大意:
输入一个 t 表示有 t 组样例,对于每组样例,输入一个n,接下来输入n个数,a1,a2,a3…an 要求输出(a[i] + a[j]) ^ a[k] 的最大值。(i != j != k)
解题思路:
套用字典树的模板,简单的插入删除操作,注意开long long ,每次不要忘记初始化。
Code:
#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e3 + 50;
int tot;
ll val[N * 32], ch[N * 32][2], cnt[N * 32];
ll a[N];
void init()//每次初始化操作
{
tot = 1;
memset(ch, 0, sizeof ch);
memset(cnt, 0, sizeof cnt);
memset(val, 0, sizeof val);
}
void insert(ll x)
{
int u = 0;
for (int i = 31; i >= 0; i --)
{
int v = (x >> i) & 1;
if (!ch[u][v])
{
ch[tot][0] = ch[tot][1] = 0;
ch[u][v] = tot++;
val[tot] = 0;
}
u = ch[u][v];
cnt[u] ++;
}
val[u] = x;
}
ll query(ll x)
{
int u = 0;
for (int i = 31; i >= 0; i --)
{
int v = (x >> i) & 1;
if (ch[u][v ^ 1] && cnt[ch[u][v ^ 1]]) u = ch[u][v ^ 1];//尽可能大,就要使每一位尽可能不相同
else u = ch[u][v];
}
return val[u];
}
void update(ll x, int c)//增加/删除操作
{
int u = 0;
for (int i = 31; i >= 0; i --)
{
int v = (x >> i) & 1;
u = ch[u][v];
cnt[u] += c;
}
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
init();
int n;
cin >> n;
for (int i = 1; i <= n; i ++)
{
cin >> a[i];
insert(a[i]);
}
ll ans = -1;
for (int i = 1; i < n; i ++)
{
for (int j = i + 1; j <= n; j ++)
{
update(a[i], -1);//为了使三者不同先减去再加上
update(a[j], -1);
ans = max(ans, (a[i] + a[j]) ^ query(a[i] + a[j]));
update(a[i], 1);
update(a[j], 1);
}
}
cout << ans << endl;
}
return 0;
}