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 ...
随机推荐
- phpstrom 快捷使用方式
file-settings--key map键映射-->collapse(代码折叠)---- ctrl (减减)2次全部折叠 (加加)2次全部展开 格式化代码 ctrl+alt+L 替换 c ...
- Struts2配置文件动态传参
两个Action动态传递参数: 1.从一个Action传递到另一个Action如果带有参数那么result中的type属性需要使用redirect 第一个Action代码如下: private Str ...
- HUST 1352 Repetitions of Substrings(字符串)
Repetitions of Substrings Description The “repetitions” of a string S(whose length is n) is a maximu ...
- PHP关于数组缓存JSON、serialize、var_export的说明
1.JSONJSON缓存变量主要是使用json_encode和json_decode函数,其中json_encode可以将变量变成文本格式存储到文件. // Store cache file_put_ ...
- KVM 虚拟化基本搭建
KVM虚拟化技术 KVM是基于x86架构上Linux操作系统的全虚拟化解决方案 ,在Centos6.3系统中,kvm已经被集成到内核中,相当于使用内核来做虚拟机管理程序.由于KVM本身就工作于内核环境 ...
- 与导航栏下控件的frame相关的edgesForExtendedLayout、translucent、extendedLayoutIncludesOpaqueBars、automaticallyAdjustsScrollViewInsets等几个属性的详解
在引入了导航控制器UINavigationController和分栏控制器UITabBarController之后,我们在设置控件的frame的时候就需要注意避开导航栏UINavigationBar ...
- Jekyll: .md to .html with self defined themes..
theme is from here $ gem install jekyll bundler ~ $ jekyll new my-awesome-site ~ $ cd my-awesome-sit ...
- DNS 域名系统 (Domain Name System)
DNS 域名系统 (Domain Name System) 许多应用层软件经常直接使用域名系统 DNS (Domain Name System),但计算机的用户只是间接而不是直接使用域名系统. 因 ...
- NOIP2014提高组第二题联合权值
还是先看题吧: 试题描述 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 Wi ,每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 ...
- 修改index.php 清空mylog1.txt
进入编辑php文件vim index.php(无则新建) -->进入命令行模式--输入a(append)-->进入编辑模式-->编辑好-->esc退出编辑模式-->:q! ...