[HDU-5536] Chip Factory (01字典树)
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:
\]
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 的序列,要你求出这个序列中
\]
的最大值即可,其中 i != j != k;
此题就是一个带修改的 01字典树 的模板。
在考虑某一个数的时候要将它自己先暂时删去。
相对于常规模板,带修改的01字典树只需我们多加一个数组。
\]
用于储存当值为当前这个节点的值的元素数量。
只有当一个节点的 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字典树)的更多相关文章
- hdu 5536 Chip Factory (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...
- HDU 5536 Chip Factory 【01字典树删除】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536 Chip Factory Time Limit: 18000/9000 MS (Java/Ot ...
- HDU 5536 Chip Factory (暴力+01字典树)
<题目链接> 题目大意: 给定一个数字序列,让你从中找出三个不同的数,从而求出:$\max_{i,j,k} (s_i+s_j) \oplus s_k$的值. 解题分析:先建好01字典树,然 ...
- HDU 5536 Chip Factory 字典树+贪心
给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...
- hdu 4825 && acdream 1063 01字典树异或问题
题意: 给一个集合,多次询问,每次给一个k,问你集合和k异或结果最大的哪个 题解: 经典的01字典树问题,学习一哈. 把一个数字看成32位的01串,然后查找异或的时候不断的沿着^为1的路向下走即可 # ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- hdu 5536 Chip Factory 字典树+bitset 铜牌题
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- HDU 5536 Chip Factory
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
随机推荐
- mybatis 部署日志
<!-- 打印查询语句 --> <setting name="logImpl" value="STDOUT_LOGGING" /> &l ...
- HTML iframe框架
iframe 作用: 就是在一个网页插入一个小窗口 窗口里面也是一个网页 <a href="http://www.baidu.com" target="da1& ...
- 洛谷 P1340 兽径管理
题目描述 约翰农场的牛群希望能够在 N 个(1<=N<=200) 草地之间任意移动.草地的编号由 1到 N.草地之间有树林隔开.牛群希望能够选择草地间的路径,使牛群能够从任一 片草地移动到 ...
- UWP中获取Encoding.Default
Encoding.GetEncoding(0); 即可
- js 判断是什么浏览器、是否为谷歌浏览器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...
- 历史管理 onhashchange
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Qt+事件的接收和忽略
事件的接收与忽略的示意图如下图: 依据前面的知识,事件是可以依据情况进行接收和忽略的,事件的传播是组件层次上面的,而不是依靠类继承机制.在一个特殊的情形下,我们必须使用accept()和ignore( ...
- AOP日志组件 多次获取post参数
AOP日志组件 多次获取post参数 需求:新增接口日志组件.通过拦截器对接口URL进行拦截处理,然后将接口post请求的参数与结果,写入日志表. 问题:POST方法的参数是存储在request.ge ...
- tkinter学习-菜单与画布
阅读目录 Menu 菜单控件 Menubutton 菜单按钮控件 OptionMenu 选项菜单 Canvas 画布控件 Menu: 说明:菜单控件,显示菜单栏,下拉菜单和弹出菜单 属性:创建一个顶级 ...
- PWA介绍
https://codelabs.developers.google.com/codelabs/your-first-pwapp/#0 PWA是一些技术的集合.用于消除web与其他客户端之间的差距,最 ...