题目传送门: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 ;
}

最新文章

  1. GCD
  2. morse code
  3. C# 使用Trace记录程序日志
  4. silverlight Canvas、StackPanel、Grid三者之间的关系
  5. 最简单的方式理解Vue的自定义指令与混合
  6. ios auto layout demystified (一)
  7. web双机热备添加心跳检测ip的时候填了网关导致外网ip不能上网
  8. noip 2009 细胞分裂
  9. Contest - 2014 SWJTU ACM 手速测试赛(2014.10.31)
  10. 讲一个使用jquery-slick旋转木马效果插件案例
  11. 用CSS画一个带阴影的三角形的示例代码
  12. Scrum冲刺阶段7
  13. 20165319 2017-2018-2《Java程序设计》课程总结
  14. 深入理解String类详解
  15. Python序列化之Json基础
  16. byte[]-&gt;new String(byte[]) -&gt; getByte()引发的不一致问题
  17. MySQL的体系结构图
  18. December 25th 2016 Week 53rd Sunday
  19. System.Reflection 获取描述
  20. Codeforces Round #222 (Div. 1) 博弈 + dp

热门文章

  1. Android报错
  2. rem.js的用法及在浏览器端的适配
  3. Docker:网络模式详解
  4. oracle12C--DG搭建配置
  5. svn server配置与TortoiseSVN、Ankhsvn+VS使用
  6. TOJ 1023 Taxi Cab Scheme
  7. nyoj 211——Cow Contest——————【floyd传递闭包】
  8. [转]C# - JSON详解
  9. tomcat一个IP绑定多个域名,不同域名访问不同的应用
  10. 深入Java字符串