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;
}

[HDU-5536] Chip Factory (01字典树)的更多相关文章

  1. hdu 5536 Chip Factory (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...

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

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536 Chip Factory Time Limit: 18000/9000 MS (Java/Ot ...

  3. HDU 5536 Chip Factory (暴力+01字典树)

    <题目链接> 题目大意: 给定一个数字序列,让你从中找出三个不同的数,从而求出:$\max_{i,j,k} (s_i+s_j) \oplus s_k$的值. 解题分析:先建好01字典树,然 ...

  4. HDU 5536 Chip Factory 字典树+贪心

    给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...

  5. hdu 4825 && acdream 1063 01字典树异或问题

    题意: 给一个集合,多次询问,每次给一个k,问你集合和k异或结果最大的哪个 题解: 经典的01字典树问题,学习一哈. 把一个数字看成32位的01串,然后查找异或的时候不断的沿着^为1的路向下走即可 # ...

  6. HDU 5536 Chip Factory 字典树

    Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  7. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

  8. hdu 5536 Chip Factory 字典树+bitset 铜牌题

    Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  9. HDU 5536 Chip Factory

    Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

随机推荐

  1. Python安装第三方库文件工具——pip

    Python安装第三方库文件一般使用pip. 1.pip的安装 (1)下载pip 进入https://pypi.python.org/pypi/pip#downloads

  2. Spring MVC异常统一处理(包括普通请求异常以及ajax请求异常)

    通常SpringMVC对异常的配置都是返回某个jsp视图给用户,但是通过ajax方式发起请求,即使发生异常,前台也无法获得任何异常提示信息.因此需要对异常进行统一的处理,对于普通请求以及ajax请求的 ...

  3. phantomas参数选项

    PhantomJS-based web performance metrics collector phantomas <url> [options] General options: - ...

  4. Xcode4删除文件后missing file警告

    1.运行终端,执行命令行进入missing file目录,然后运行 svn delete nameOfMissingFile 或 git rm nameOfMissingFile 2.删除隐藏的.sv ...

  5. helm istio k8s docker

    helm https://hub.helm.sh/ k8s https://www.kubernetes.org.cn/k8s istio 微服务 https://istio.io/

  6. 毛毛虫组【Beta】Scrum Meeting 3

    第三天 日期:2019/6/25 前言 第三次会议: 时间:6月25日 地点:教10-A511 内容:此次会议主要是对项目验收做准备工作. 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: ...

  7. pre-receive hook declined

    没有master分支的代码提交权限. 分配权限或者提交分支合并申请

  8. PLAYGROUND 可视化

    PLAYGROUND 可视化 由 王巍 (@ONEVCAT) 发布于 2015/09/23 在程序界,很多小伙伴都会对研究排序算法情有独钟,并且试图将排序执行的过程可视化,以便让大家更清晰直观地了解算 ...

  9. Emmet:HTML/CSS代码快速编写神器--20150422

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...

  10. 51nod 1265 四点共面——计算几何

    题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1265 以其中某一点向其它三点连向量,若四点共面,这三个向量定义的平行六面体 ...