Chip Factory
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 5363 Accepted Submission(s): 2409
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 test cases with n>100
Output
For each test case, please output an integer indicating the checksum number in a line.
Sample Inpu
2
3
1 2 3
3
100 200 300
Sample Output
6
400
Souce
2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)
[题意]
max( (s[i] + s[j] ) ^ s[k] )
对 串 构建 Tire 树 ,然后 在 tire 树上 贪心 (s[i] + s[j] )
每次 删去, 在加上..
构建Tire 树得目的在于 01 字典树, 这样 二进制下 重复的 许多. 尽可能得 是 他进位.
因为进位越大. 异或 结果 就可能越大.
贪心时, 让他尽可能得去对立面.
代码:
#include <bits/stdc++.h>
#include <stdio.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
typedef long long ll;
const int maxn = 1e5+10;
const int mod =1e9+7;
const int inf = 0x3f3f3f3f;
using namespace std;
const int root = 0;
ll a[maxn];
struct Tire{
int sz;
ll next[2];
}tire[maxn];
ll num;
void insert(int v)
{
int now = root;
int dir;
tire[now].sz ++;
for(int i = 30;i>=0;i--)
{
if( v&(1<<i) ) // 为1
dir = 1;
else
dir = 0;
if( !tire[now].next[dir] )
tire[now].next[dir] = ++num;
now = tire[now].next[dir];
tire[now].sz ++;
}
}
void del(int v)
{
int now = root;
int dir;
tire[now].sz --;
for(int i = 30 ;i>=0;i--)
{
if( v&(1<<i) )
dir = 1;
else
dir = 0;
now = tire[now].next[dir];
tire[now].sz --;
}
}
int query(int v)
{
int now = root;
int dir;
for(int k = 30; k>=0 ; k--)
{
if( v&(1<<k) ) // 是否为1
dir = 1;
else
dir = 0;
if( dir )
{
if( tire[now].next[0] && tire[ tire[now].next[0] ].sz )
now = tire[now].next[0];
else
{
now = tire[now].next[1];
v^= (1<<k);
}
}
else
{
if( tire[now].next[1] && tire[ tire[now].next[1] ].sz )
{
now = tire[now].next[1];
v ^= (1<<k);
}
else
{
now = tire[now].next[0];
}
}
}
return v;
}
int main(int argc, char const *argv[])
{
int T;
scanf("%d",&T);
while(T--)
{
memset(tire,0,sizeof(tire));
memset(a,0,sizeof(a));
num = 1;
int ans = -1;
int n;
scanf("%d",&n);
rep(i,1,n)
{
scanf("%lld",&a[i]);
}
rep(i,1,n)
insert(a[i]);
for(int i = 1; i <=n; i++)
{
del(a[i]);
for(int j = i+1; j<=n; j++)
{
del(a[j]);
// cout<<query( a[i]+a[j] )<<endl;
ans = max(ans, query( a[i]+a[j] ) );
insert(a[j]);
}
insert(a[i]);
}
printf("%d\n", ans);
}
return 0;
}