HDU 5536 Chip Factory 【01字典树删除】

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536

Chip Factory

Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 4915    Accepted Submission(s): 2205

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
 

题目大意:

给 N 个数,在这 N 个数里找到三个值 i, j,k 使得(i+j)⊕ k 最大,输出这个最大值。

解题思路:

每次在01字典树里删除 i 和 j 然后 (i+j)和01字典树里的匹配求最大异或值。

AC code:

 #include <bits/stdc++.h>
#define ll long long int
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 1e3+;
int ch[MAXN*][];
ll value[MAXN*];
ll b[MAXN];
int num[MAXN*];
int node_cnt; void init()
{
memset(ch[], , sizeof(ch[]));
node_cnt = ;
} void Insert(ll x)
{
int cur = ;
for(int i = ; i >= ; i--){
int index = (x>>i)&;
if(!ch[cur][index]){
memset(ch[node_cnt], , sizeof(ch[node_cnt]));
ch[cur][index] = node_cnt;
value[node_cnt] = ;
num[node_cnt++] = ;
}
cur = ch[cur][index];
num[cur]++;
}
value[cur] = x;
} void update(ll x, int d)
{
int cur = ;
for(int i =; i >= ; i--){
int index = (x>>i)&;
cur = ch[cur][index];
num[cur]+=d;
}
} ll query(ll x)
{
int cur = ;
for(int i = ; i >= ; i--)
{
int index = (x>>i)&;
if(ch[cur][index^] && num[ch[cur][index^]]) cur = ch[cur][index^];
else cur = ch[cur][index];
}
return x^value[cur];
} int main()
{
int T_case, N;
scanf("%d", &T_case);
while(T_case--)
{
scanf("%d", &N);
init();
for(int i = ; i <= N; i++)
{
scanf("%lld", &b[i]);
Insert(b[i]);
}
ll ans = ;
for(int i = ; i <= N; i++){
for(int j = ; j <= N; j++){
if(i == j) continue;
update(b[i], -);
update(b[j], -);
ans = max(ans, query(b[i]+b[j]));
update(b[i], );
update(b[j], );
}
}
printf("%lld\n", ans);
}
return ;
}
上一篇:Spring学习笔记之整合struts


下一篇:ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)