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$ 个整数 $ ...
随机推荐
- 如何用dos命令运行testng
写好的自动化程序怎么让它运行呢,总不能每次都启动eclipse吧,下面就先介绍一种用dos命令运行testNG的方法. 1.把项目打成jar吧,我用的是Fat jar工具. 2.在电脑的某个盘建一个文 ...
- 边工作边刷题:70天一遍leetcode: day 88-5
coins in a line I/II/III: check above 1. recursion的返回和dp[left][right]表示什么?假设game是[left,right],那么play ...
- codeforces 712B B. Memory and Trident(水题)
题目链接: B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input ...
- .Net Framework 4.0 内部排序探索
简介 一时好奇心起,想一窥.Net Framework 4.0内部究竟是使用何种算法排序.以前听人说Framework内部是使用的快速排序,但究竟耳听为虚,眼见为实.主要通过JetBrains dot ...
- 实现Maya FEM节点
准备实现FEM节点. 发现一种让自定义的Locator以及它的变换节点自动命名的方法.代码如下: void FEMSimulationNode::postConstructor() { MFnDepe ...
- eval() 函数
eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. var str = '12+45*45'; alert(eval(str))//计算结果 还有一个重要作用可以把字符串 ...
- usb驱动开发9之设备描述符
前面分析了usb的四大描述符之端点描述符,接口描述符(每一个接口对应一个功能,与之配备相应驱动),配置描述符,最后分析设备如何包括这些描述符.首先记住,在usb的世界里,设备大于配置,配置大于接口,接 ...
- C# 无边框窗体边框阴影效果
通过下面代码在构造函数中调用方法 SetShadow(); 即可实现无边框窗体的阴影效果了 需要添加命名空间 using System.Runtime.InteropServices; private ...
- “插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题
“插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题 最近做一个前端的项目,项目需要引用VLC浏览器插件,javascript在IE.Firefox等浏览器 ...
- SQLServer如何删除字段中的某个字符串,或者替换为空格?
sql="update Table set 字段=REPLACE ( 字段,'123' , ' ') where XXX条件"把字段中123替换为空格