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:

\[max_{i,j,k}{(si+sj)}\bigoplus{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

Source

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

Recommend

hujie | We have carefully selected several similar problems for you: 6297 6296 6295 6294 6293


Solution

简述题意 :

有 q 次询问,每次给你一个长度为 n 的序列,要你求出这个序列中

\[{(si+sj)}\bigoplus{sk}
\]

的最大值即可,其中 i != j != k;


此题就是一个带修改的 01字典树 的模板。

在考虑某一个数的时候要将它自己先暂时删去。

相对于常规模板,带修改的01字典树只需我们多加一个数组。

\[num[maxn]
\]

用于储存当值为当前这个节点的值的元素数量。

只有当一个节点的 num 不为 0 时,我们才能访问。

然后我们增加一个 Change 函数。

用于改变在字典树中的元素的 num。

基本遍历过程和插入以及查询类似。

但是到达元素这个节点时,我们进行的是修改 num 操作。

Change 函数代码如下:

void change(int a,int v) //分别为这个元素的值以及对它num的变化值.
{
int u=0;
for(int i=32;i>=0;i--)
{
int c=((a>>i)&1);
u=ch[u][c];
num[u]+=d;
}
}

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1008; int ch[32*maxn][2];
ll val[32*maxn];
int num[32*maxn];
int sz;
ll b[maxn]; void init()
{
memset(ch[0],0,sizeof(ch[0]));
sz=1;
} void insert(ll a){
int u=0;
for(int i=32;i>=0;i--)
{
int c=((a>>i)&1);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
num[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
num[u]++;
}
val[u]=a;
}
void update(ll a,int d)
{
int u=0;
for(int i=32;i>=0;i--)
{
int c=((a>>i)&1);
u=ch[u][c];
num[u]+=d;
}
}
ll query(ll x)
{
int u=0;
for(int i=32;i>=0;i--)
{
int c=((x>>i)&1);
if(ch[u][c^1]&&num[ch[u][c^1]])
u=ch[u][c^1];
else u=ch[u][c];
}
return x^val[u];
} int main(){
int t;
cin>>t;
while(t--){
int n;
scanf("%d",&n);
init();
for(int i=1;i<=n;i++)
{
scanf("%lld",&b[i]);
insert(b[i]);
}
ll ans=-1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(i==j) continue;
update(b[i],-1);update(b[j],-1);
ans=max(ans,query(b[i]+b[j]));
update(b[i],1);update(b[j],1);
}
cout<<ans<<endl;
}
return 0;
}

最新文章

  1. react-native学习笔记--史上最详细Windows版本搭建安装React Native环境配置
  2. 使用sbt构建spark 程序
  3. BestCoder Round #75
  4. 解决Unable to reach a settlement: [diffie-hellman-group1-sha1, diffie-hellman-group-exchange-sha1] and [curve25519-sha256@li
  5. C++ list&lt;list&lt;int&gt; &gt;类型的对象遍历
  6. android官方侧滑菜单DrawerLayout详解
  7. Control.Invoke和Control.BeginInvoke
  8. Spring mvc 返回json格式 - 龙企阁 - 博客频道 - CSDN.NET
  9. 我的2016年终总结(PF项目框架设计心得分享 2.0rc)
  10. Ubuntu 简单安装 Docker
  11. 【Swfit】Swift与OC两种语法写单例的区别
  12. Linux 学习笔记_12_文件共享服务_2_FTP应用--vsftpd
  13. mybatis 在xml文件中获取当前时间的sql
  14. 查看Android应用包名、Activity的几个方法
  15. React-Native采坑总结
  16. Android studio使用android:style/Theme.Dialog报错:You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
  17. python—集合
  18. 如何删除pagefile.sys
  19. 03LaTeX学习系列之---TeXworks的使用
  20. 原生JS实现addClass,removeClass,toggleClass

热门文章

  1. UVA - 12264 Risk (二分,网络流)
  2. bzoj 2658
  3. CentOS7——防火墙设置
  4. linux设置http/https proxy及忽略proxy的方法
  5. IjkPlayer播放器秒开优化以及常用Option设置
  6. 毛毛虫组【Beta】Scrum Meeting 3
  7. tp5 -- 微信公众号支付
  8. Redis数据库(二)
  9. GIMP矩形选框预圆形选框
  10. 5.电影搜索之 自动填充,也叫autocomplete、搜索建议!