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 ...
随机推荐
- C++中各种容器的类型与特点
1.vector 连续存储结构,每个元素在内存上是连续的: 支持高效的随机访问和在尾端插入/删除操作,但其他位置的插入/删除操作效率低下: 2.deque 连续存储结构,即其每个元素在内存上也是连续的 ...
- Xutils的使用 转载 带自己细细研究
单例模式static DbUtils db = null; public static DbUtils getDb(Context context) { if (context == null) { ...
- C/C++文件结构
总结者:kate (1).h 为头文件:存放 版权和版本声明,预处理块 ,函数和类结构声明 (2).cpp文件:代码文件,存放程序的实现 大都数时候,源文件和头文件是对应出现的,比如有一个A.cpp ...
- 元素ID命名规范
因为本框架默认所有内容都位于一个Document中,所以为元素命名为ID的时候需要做到唯一性,如果确实不可避免的会出现有重读ID的现象,需要操作当前页片(页面片段,就是子页面)的时候,尽量用: $.C ...
- SplashTop Remote + 4核android平板 试用
局域网默认情况 最大100Mb 的网速下, 延迟在500ms+, 观看视频无影响, 但游戏无法进行! 另一种方案,利用多网卡来提升网络传输性能!
- 实例化(用new的方式)创建一个对象的顺序
父类静态块--->子类静态块----->父类普通代码块----->父类构造方法------->子类普通代码块----->子类构造方法 如果父类构造方法中调用的非priva ...
- linux下获取帮助
-h --help man 代號 代表內容 使用者在shell中可以操作的指令或可执行档 系統核心可呼叫的函数与工具等 一些常用的函数(function)与函数库(library),大部分是C的函数库 ...
- 理解Mach Port
参考文档: 1. http://robert.sesek.com/thoughts/2012/1/debugging_mach_ports.html 2. Mach 3 Kernel Interfac ...
- 捕获异常的两种方式Exception
1.抛出异常:让调用此方法的代码去管 public static void GetFile() throws Exception{} package com.throwable; import jav ...
- linux下IPTABLES配置详解
如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...