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 ...
随机推荐
- ProcessOn:功能强大的在线作图工具(HTML5)
ProcessOn是一款专业作图人员的社交网络,这里汇聚很多业界专家.学者,同时他们分享的作品又形成一个庞大的知识图库,你在学习专业知识的同时还可以结交一些志同道合的新朋友. ProcessOn核心设 ...
- C语言输出规定长度的整数,不够位数前面补零
今天在做ACM题目的时候,遇到了这么一个问题,还真别说,这个以前真的没用过,当时就傻掉了,还好这个世界有Google,通过搜索了解了输出这种格式的C语言实现方法.但是没有找到C++的实现方法,希望知道 ...
- ODI中web service介绍
ODI WS架构
- jQuery get post 碎片(远程html)加载
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- php的查询数据
php中 连接数据库,通过表格形式输出,查询数据.全选时,下面的分选项都选中;子选项取消一个时,全选按钮也取消选中. <!DOCTYPE html PUBLIC "-//W3C//DT ...
- c规范(1)
1文件结构 头文件.h 保存文件声明 定义文件.c 程序实现 2版本标示 用注释 (1)版权信息. (2)文件名称,标识符,摘要. (3)当前版本号,作者 修改者,完成日期. (4)版本历史信息. ...
- struts2DMI(动态方法调用)
DMI(Dynamic Method Invoke)即动态,是strus2的一个特性,我们知道,在最开始学习strus2时,往往一个action中只有一个excute方法,比如说add,delete, ...
- 中国省市 JS代码
很实用的一段JS代码, 用户注册的时候,选择地址常用到.代码如下: <script language="javascript"> var g_selProvince; ...
- 拆解cytom!c's 的keyFile保护
系统 : Windows xp 程序 : cytom!c's 程序下载地址 :http://pan.baidu.com/s/1nulAYBv 要求 : 伪造KeyFile 使用工具 :IDA & ...
- HDU 2157
http://acm.hdu.edu.cn/showproblem.php?pid=2157 求A到B经过K个点的方案数 http://www.matrix67.com/blog/archives/2 ...