HDU-5536 Chip Factory (字典树)
题目大意:给n个数,编号为1~n,取三个编号不同的数,使表达式(a+b)^c的值最大。
题目分析:将这n个数按二进制位建立一棵trie。枚举i、j的和,查询亦或最大值,但在查询之前要把i、j在trie中删除,查询完毕后再插入trie。
ps:用数组实现trie会超时,因为每次test case之前都要初始化数组,非常耗时。
代码如下:
# include<iostream>
# include<cstdio>
# include<cmath>
# include<vector>
# include<list>
# include<queue>
# include<map>
# include<set>
# include<cstring>
# include<algorithm>
using namespace std;
# define LL long long const int N=1000;
const int INF=1000000000;
const double inf=1e20; struct Node
{
int cnt;
Node *son[2];
Node(){
cnt=0;
son[0]=son[1]=NULL;
}
};
Node *root;
int a[N+5];
int n; void update(int x)
{
Node *p=root;
for(int i=30;i>=0;--i){
int k=(x&(1<<i))>0?1:0;
if(p->son[k]==NULL)
p->son[k]=new Node();
++(p->son[k]->cnt);
p=p->son[k];
}
} void erase(LL x)
{
Node *p=root;
for(int i=30;i>=0;--i){
int k=(x&(1<<i))>0?1:0;
--(p->son[k]->cnt);
p=p->son[k];
}
} int query(int x)
{
int res=0;
Node *p=root;
for(int i=30;i>=0;--i){
int k=(x&(1<<i))>0?1:0;
if(p->son[k^1]!=NULL&&p->son[k^1]->cnt>0){
res|=(1<<i);
p=p->son[k^1];
}else{
p=p->son[k];
}
}
return res;
} void delTree(Node *p)
{
if(p==NULL) return ;
delTree(p->son[0]);
delTree(p->son[1]);
delete p;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
root=new Node();
for(int i=0;i<n;++i){
scanf("%d",a+i);
update(a[i]);
}
int ans=0;
for(int i=0;i<n;++i){
erase(a[i]);
for(int j=i+1;j<n;++j){
erase(a[j]);
ans=max(ans,query(a[i]+a[j]));
update(a[j]);
}
update(a[i]);
}
printf("%d\n",ans);
delTree(root);
}
return 0;
}
HDU-5536 Chip Factory (字典树)的更多相关文章
- HDU 5536 Chip Factory 字典树+贪心
给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- 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 【01字典树删除】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536 Chip Factory Time Limit: 18000/9000 MS (Java/Ot ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- HDU 5536 Chip Factory (暴力+01字典树)
<题目链接> 题目大意: 给定一个数字序列,让你从中找出三个不同的数,从而求出:$\max_{i,j,k} (s_i+s_j) \oplus s_k$的值. 解题分析:先建好01字典树,然 ...
- hdu5536 Chip Factory 字典树+暴力 处理异或最大 令X=(a[i]+a[j])^a[k], i,j,k都不同。求最大的X。
/** 题目:hdu5536 Chip Factory 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:给定n个数,令X=(a[i]+a[j] ...
- 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
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- 2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
随机推荐
- mysql中Access denied for user 'root'@'localhost' (using password:YES)(zhuan)
错误代码 1045Access denied for user 'root'@'localhost' (using password:YES) 如果你的mysql也出现以上这种提示, 建议你逐个字看完 ...
- DotNetBar v12.1.0.0 Fully Cracked
更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.1.0.0 如果遇到破解问题可以与我 ...
- iphone获取当前磁盘信息
获取iphone磁盘总大小.已使用空间.空闲空间 [代码]悦德财富:https://www.yuedecaifu.com 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- 收藏的博客--Ogre
1.Ogre一些东西和流程深入讲解 http://blog.csdn.net/yanonsoftware/article/category/226048 2.Ogre天龙八部分析 http://bl ...
- 为自己的系统定制openstack ceilometer
一.目的 最近研究了一下ceilometer,目的做一个监控系统,对系统中发生的事件进行处理.ceilometer对openstack各个组件信息的收集方式主要由 推 和 拉 两种. “推”: 就是 ...
- BZOJ 2241 打地鼠
暴力. 这怎么这么快.... #include<iostream> #include<cstdio> #include<cstring> #include<a ...
- IOS 作业项目(4)步步完成 画图 程序(上)
先上流程图
- html input
disabled="disabled" <input name="" type="checkbox" value="&quo ...
- jquery 轮播图
slider.js (function(){ /** parent //父容器 changeTime //每次间隔几秒切换下一条 leaveTime //鼠标从小图上离开过后几秒继续切换 index ...
- HDU 1350
http://acm.hdu.edu.cn/showproblem.php?pid=1350 给m个顾客的乘车信息,表示几点前上车,要从一个坐标点到达另一个坐标点,花费的时间是两点的曼哈顿距离,两次换 ...