hdu5536 字典树xor
一想到xor还要求最大类似的题,字典树效率高。
此代码c++ TLE。
#include<stdio.h>
#include<string.h>
const int maxn = ;
struct node
{
int num;
node *next[];
void init()
{
next[]=next[]=NULL;
num=;
}
};
node *trie,*p=NULL;
int a[maxn],b[maxn],n;
int max(int x,int y)
{
return x>y?x:y;
}
void init()
{
trie=new node;
trie->init();
}
void add(int x)
{
p=trie;
int i,j;
for(i=;i>=;i--)
{
int t;
if(x&(<<i))
t=;
else t=;
if(t)
{
if(p->next[t]==NULL)
{
node *q=new node;
q->init();
p->next[t]=q;
}
p=p->next[t];
p->num++;
}
else
{
if(p->next[t]==NULL)
{
node *q=new node;
q->init();
p->next[t]=q;
}
p=p->next[t];
p->num++;
}
}
}
void Del(int x)
{
p=trie;
int i;
for(i=;i>=;i--)
{
int t;
if(x&(<<i))
t=;
else t=;
p=p->next[t];
p->num--;
}
}
int query(int x)
{
p=trie;
int fx=;
for(int i=;i>=;i--)
{
int t;
if(x&(<<i))t=;
else t=;
if(t)
{
if(p->next[]!=NULL&&p->next[]->num!=)
{
p=p->next[];
}
else
{
fx=fx^(<<i);
p=p->next[];
}
}
else
{
if(p->next[]!=NULL&&p->next[]->num!=)
{
fx=fx^(<<i);
p=p->next[];
}
else p=p->next[];
}
}
return fx;
}
void Del(node *trie)
{
for(int i=;i<=;i++)
{
if(trie->next[i])
Del(trie->next[i]);
}
delete trie;
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
add(a[i]);
}
int ans=;
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
if(i==j)
continue;
Del(a[i]);
Del(a[j]);
int fp=a[i]+a[j];
int ret=query(fp);
//printf("%d ",ret);
ans=max(ans,fp^ret);
add(a[i]);
add(a[j]);
}
}
printf("%d\n",ans);
Del(trie);
}
}
hdu5536 字典树xor的更多相关文章
- hdu4825 字典树 XOR
用字典树思想来做.对于一个数,给出他的二进制,然后更具二进制建立字典树,然后每次询问的时候的数也给出二进制,如果当前为1,那就向0走,为0,向1走. #include<stdio.h> # ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- 字典树-百度之星-Xor Sum
Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...
- HDU 4825 Xor Sum 字典树+位运算
点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- 2014百度之星第三题Xor Sum(字典树+异或运算)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- HDU--5269 ZYB loves Xor I (字典树)
题目电波: HDU--5269 ZYB loves Xor I 首先我们先解决 ai xor aj 每个数转化为二进制 我们用字典树统计 每个节点 0 和 1 的出现的个数 #include< ...
- HDU--4825 Xor Sum (字典树)
题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...
- 51nod 1295 XOR key 可持久化01字典树
题意 给出一个长度为\(n\)的正整数数组\(a\),再给出\(q\)个询问,每次询问给出3个数,\(L,R,X(L<=R)\).求\(a[L]\)至\(a[R]\)这\(R-L+1\)个数中, ...
- CH 1602 - The XOR Largest Pair - [字典树变形]
题目链接:传送门 描述在给定的 $N$ 个整数 $A_1, A_2,\cdots,A_N$ 中选出两个进行xor运算,得到的结果最大是多少? 输入格式第一行一个整数 $N$,第二行 $N$ 个整数 $ ...
随机推荐
- C++ macro(宏)使用小结
谈起C++中的宏,我们第一个想到的应该就是“#define”,它的基本语法长得像这样: #define macroname(para1, para2, para3, ... ,paran) macro ...
- 51nod-1661 1661 黑板上的游戏(组合游戏)
题目链接: 1661 黑板上的游戏 Alice和Bob在黑板上玩一个游戏,黑板上写了n个正整数a1, a2, ..., an,游戏的规则是这样的:1. Alice占有先手主动权.2. 每个人可以选取一 ...
- 如何在HTML不同的页面中,共用头部与尾部?
一.asp语言和PHP语言 首先制作一个头部文件head.asp,或者一个底部文件foot.asp.如主页是index.asp,调用头部代码是在index.asp文件代码的开始位置(第一个标记后面,& ...
- Android代码优化----PullToRefresh+universal-image-loader实现从网络获取数据并刷新
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- AC日记——积木大赛 洛谷 P1969
题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...
- AutoIT删除Internet临时文件
搜集了几个超赞的方法: 1.删除临时文件 Temporary Internet Files:RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 2. 删 ...
- Android Studio中提示:Project SDK is not defined
Android Studio中提示:Project SDK is not defined 2015 年 4 月 1 日 下午 9:04crifan已有2209人围观我来说几句 [背景] 之前用Andr ...
- mysqli_stmt预处理类
<?php $mysqli=new mysqli("localhost", "root", "123456", "xsph ...
- jacob下载问题, Office word 此文件正由另一应用程序或用户使用的解决方法
http://jingyan.baidu.com/article/75ab0bcbd6682fd6864db2db.html
- iBatis.net入门指南
iBatis.net入门指南 - 1 - 什么是iBatis.net ? - 3 - iBatis.net的原理 - 3 - 新人指路 - 3 - iBatis.net的优缺点 ...