hdu 5536 xor题
input
1<=T<=1000
3<=n<=1000
s1 s2 ... sn 0<=si<=10e9
最多十个样例n>=100
output
max((a[i]+a[j])^a[k]) i!=j!=k
做法,将s数组建成一颗01字典树,枚举a[i]+a[j]找最大,找之前要把a[i]和a[j]删掉,找完后再插入
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#define bit 31 using namespace std; struct node
{
int c,l,r;
}; node a[];
int s[],n,T; void erase(int x)
{
int u=;
for(int i=bit;i>=;i--)
{
if(x&(<<i))
{
u=a[u].r;
a[u].c--;
}
else
{
u=a[u].l;
a[u].c--;
}
}
} void insert(int x)
{
int u=;
for(int i=bit;i>=;i--)
{
if(x&(<<i))
{
u=a[u].r;
a[u].c++;
}
else
{
u=a[u].l;
a[u].c++;
}
}
} int find(int x)
{
int ans=,u=;
for(int i=bit;i>=;i--)
{
if(x&(<<i))//bit位为1
{
if(a[u].l&&a[a[u].l].c)//能往左走就加1<<i
{
ans+=<<i;
u=a[u].l;
}
else u=a[u].r;//不能往左走就往右走
}
else
{
if(a[u].r&&a[a[u].r].c)
{
ans+=<<i;
u=a[u].r;
}
else u=a[u].l;
}
}
return ans;
} int main()
{
freopen("/home/user/桌面/in","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(a,,sizeof(a[])*(n+)*);
int root,idx=,maxd=-;
for(int i=;i<n;i++)//建树
{
scanf("%d",&s[i]);
root=;
for(int j=bit;j>=;j--)
{
if(s[i]&(<<j))
{
if(!a[root].r) a[root].r=idx++;
root=a[root].r;
a[root].c++;
}
else
{
if(!a[root].l) a[root].l=idx++;
root=a[root].l;
a[root].c++;
}
}
}
// for(int i=0;i<idx;i++) printf("%d %d %d\n",a[i].c,a[i].l,a[i].r);
// printf("%d\n",idx);
for(int i=;i<n;i++)
{
erase(s[i]);
for(int j=i+;j<n;j++)
{
erase(s[j]);
maxd=max(find(s[i]+s[j]),maxd);
insert(s[j]);
}
insert(s[i]);
}
printf("%d\n",maxd);
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return ;
}
hdu 5536 xor题的更多相关文章
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- HDU 3949 XOR [线性基|高斯消元]
目录 题目链接 题解 代码 题目链接 HDU 3949 XOR 题解 hdu3949XOR 搞死消元找到一组线性无关组 消出对角矩阵后 对于k二进制拆分 对于每列只有有一个1的,显然可以用k的二进制数 ...
- HDU-1042-N!(Java大法好 && HDU大数水题)
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Subm ...
- HDU 3949 XOR(高斯消元搞基)
HDU 3949 XOR pid=3949" target="_blank" style="">题目链接 题意:给定一些数字,问任取几个异或值第 ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- hdu 4825 Xor Sum(01字典树模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...
- HDU 4825 Xor Sum (模板题)【01字典树】
<题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- hdu 5536 Chip Factory 字典树+bitset 铜牌题
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
随机推荐
- ggplot2 theme相关设置—线条设置
在ggplot的主题射中有一部分图需要对图中的部分线条进行设置 element_line(colour = NULL, size = NULL, linetype = NULL, lineend = ...
- 使用EasyBCD 从硬盘安装 deepin2014.1
EasyBCD config code: title Install Deepin2014 root (hd0,1) kernel (hd0,1)/vmlinuz boot=casper iso-s ...
- hdu_5507_GT and strings(AC自动机)
题目链接:hdu_5507_GT and strings 题意:给n个字符串和q个询问,每个询问给两个数字x,y,问1.x是否为y的子序列,2.x是否为y的子串,是输出1,否则输出0,每个询问输出2个 ...
- 浙大pat 1035题解
1035. Password (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To prepare f ...
- 游戏Demo(持续更新中...)
格斗游戏 主要用于联系Unity的动画系统,并加入了通过检测按键触发不同的技能. WASD控制方向,AD为技能1,SW为技能2,右键跳跃,连续单机普通连招. 本来是要用遮罩实现跑动过程中的攻击动作,但 ...
- mac 上面安装mysql-python
安装过程中一直报错: EnvironmentError: mysql_config not found 最终下面的方式解决: 58down voteaccepted +200 Ok, well, fi ...
- IOS 类似于网易新闻首页新闻轮播的组件
一.需求分析 1.可横向循环滚动新闻图片 2.滚动到对应图片时显示新闻标题 3.每张新闻图片可点击 4.有pageControl提示 5.具有控件的扩展能力 二.设计实现 1.显示图片使用SDWebI ...
- Python之Socket&异常处理
Socket Socket用于描述IP地址和端口号,每个应用程序都是通过它来进行网络请求或者网络应答. socket模块和file模块有相似之处,file主要对某个文件进行打开.读写.关闭操作.soc ...
- LeetCode OJ 153. Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- NOIP2014-普及组复赛-第三题-螺旋矩阵
题目描述 Description 一个n行n列的螺旋矩阵可由如下方法生成: 从矩阵的左上角(第1行第1列)出发,初始时向右移动:如果前方是未曾经过的格子,则继续前进,否则右转:重复上述操作直至经过矩阵 ...