一想到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的更多相关文章

  1. hdu4825 字典树 XOR

    用字典树思想来做.对于一个数,给出他的二进制,然后更具二进制建立字典树,然后每次询问的时候的数也给出二进制,如果当前为1,那就向0走,为0,向1走. #include<stdio.h> # ...

  2. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  3. 字典树-百度之星-Xor Sum

    Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...

  4. HDU 4825 Xor Sum 字典树+位运算

    点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  5. 2014百度之星第三题Xor Sum(字典树+异或运算)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  6. HDU--5269 ZYB loves Xor I (字典树)

    题目电波: HDU--5269 ZYB loves Xor I 首先我们先解决 ai xor aj 每个数转化为二进制  我们用字典树统计 每个节点 0 和 1 的出现的个数 #include< ...

  7. HDU--4825 Xor Sum (字典树)

    题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...

  8. 51nod 1295 XOR key 可持久化01字典树

    题意 给出一个长度为\(n\)的正整数数组\(a\),再给出\(q\)个询问,每次询问给出3个数,\(L,R,X(L<=R)\).求\(a[L]\)至\(a[R]\)这\(R-L+1\)个数中, ...

  9. CH 1602 - The XOR Largest Pair - [字典树变形]

    题目链接:传送门 描述在给定的 $N$ 个整数 $A_1, A_2,\cdots,A_N$ 中选出两个进行xor运算,得到的结果最大是多少? 输入格式第一行一个整数 $N$,第二行 $N$ 个整数 $ ...

随机推荐

  1. 二分套二分 hrbeu.acm.1211Kth Largest

    Kth Largest TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Description There are two sequences A and ...

  2. AutoIT 实现Firefox下载

    Firefox下载的完整代码: Func IsVisible($handle) ;WinGetState: 2 = Window is visible If BitAND(WinGetState($h ...

  3. Nginx反向代理+负载均衡简单实现(http方式)

    1)nginx的反向代理:proxy_pass2)nginx的负载均衡:upstream 下面是nginx的反向代理和负载均衡的实例: 负载机:A机器:103.110.186.8/192.168.1. ...

  4. ls -F一种非常有用的ls格式

    ls -F一种非常有用的ls格式  tz/y/yupeng > ls -F#q#           News/         doc/          images/       mbox ...

  5. win安装mysql5.1

    https://dev.mysql.com/downloads/mysql/5.5.html 这里官网下载5.5的安装 我装了几次5.1的,不知道系统有问题还是咋滴,重启mysql服务启动不起来了.擦 ...

  6. Java系列:关于Java中的桥接方法

    这两天在看<Java核心技术 卷1>的泛型相关章节,其中说到了在泛型子类中override父类的泛型方法时,编译器会自动生成一个桥接方法,这块有点看不明白. 书上的例子代码如下: publ ...

  7. [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

    4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...

  8. linux内核分析 期中总结

    LINUX内核分析 链接汇总 LINUX内核分析第一周学习总结——计算机是如何工作的 LINUX内核分析第二周学习总结——操作系统是如何工作的 LINUX内核分析第三周学习总结——构造一个简单的Lin ...

  9. c#中的var优缺点和适用场景

    var是c# 3.0新加的特性,叫做隐式类型局部变量,大家都知道c#其实是一种强类型的语言,为什么会引入匿名类型呢? 我猜测是因为linq的原因吧,因为感觉var在linq中被大量使用.下面说下var ...

  10. 如何抛出未认证(Unauthorized=401)异常代码

    throw new System.ServiceModel.Web.WebFaultException<string>("\"mes\":\"认证信 ...