HDU-5536 Chip Factory,又见字典树,好题+1!
Chip Factory
题意:一个n个数的数列,求三个数其中两个数的和与另外一个数的异或值最大,输出这个最大值。
思路:和前面那个百度之星资格赛HDU4825的类似,多了两个过程,一个是枚举和,另一个是删除过程,第一次写删除操作,还一遍A了,小激动。
会不会是后台水,东北师范官方没有给出标准测试数据,也就是说这数据是hdu YY的,我猜是这样,碰到过类似的情况,错误的代码竟然A了区域赛的题。这样为了AC貌似有点不好呀,不过思路很值得学习!
struct tree
{
int f;
tree *next[N];
tree()
{
for(int i=0; i<N; i++) next[i]=NULL;
f=0;
}
}*root;
int a[33];
ll s[1005];
void ch(ll x)//转化为二进制
{
memset(a,0,sizeof(a));
int len=0;
while(x)
{
a[len++]=x%2;
x/=2;
}
}
void insert(ll x)//插入一个数
{
ch(x);
int len=32;//长度最多32位
tree *p=root;
while(len>=0)
{
int id=a[len];
if(p->next[id]==NULL)
{
tree *temp=new tree;
p->next[id]=temp;
}
p=p->next[id];
p->f++;//每个点都构成一个数,经过此点,次数加一
len--;
}
}
ll query(ll x)
{
ch(x);
tree *p=root;
for(int i=0; i<33; i++) a[i]^=1;//不一定要这样,只是写HDU4825的时候用的这种思路
int len=32;
ll ans=0;
while(len>=0)
{
int id=a[len];
if(p->next[id]==NULL||p->next[id]->f==0) id^=1;//为0表示此节点被撤销了,另寻他路
if(id) ans+=(ll)1<<len;
p=p->next[id];
len--;
}
return ans^x;
}
void fre(ll x)
{
ch(x);
int len=32;
tree *p=root;
while(len>=0)
{
int id=a[len];
p->next[id]->f--;//撤销路径
p=p->next[id];
len--;
}
}
void del(tree *root)
{
for(int i=0; i<N; i++)
if(root->next[i])
del(root->next[i]);
delete(root);
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
root=new tree;
for(int i=0; i<n; i++)
{
scanf("%I64d",&s[i]);
insert(s[i]);
}
ll ans=0;
for(int i=0; i<n; i++)
{
fre(s[i]);
for(int j=i+1; j<n; j++)
{
fre(s[j]);
ans=max(ans,query(s[i]+s[j]));//撤销点,枚举和
insert(s[j]);
}
insert(s[i]);
}
printf("%I64d\n",ans);
del(root);
}
return 0;
}
HDU-5536 Chip Factory,又见字典树,好题+1!的更多相关文章
- HDU 5536 Chip Factory 【01字典树删除】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536 Chip Factory Time Limit: 18000/9000 MS (Java/Ot ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- HDU 5536 Chip Factory (暴力+01字典树)
<题目链接> 题目大意: 给定一个数字序列,让你从中找出三个不同的数,从而求出:$\max_{i,j,k} (s_i+s_j) \oplus s_k$的值. 解题分析:先建好01字典树,然 ...
- HDU 5536 Chip Factory 字典树+贪心
给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- hdu 5536 Chip Factory 字典树+bitset 铜牌题
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- Chip Factory(01字典树)
Chip Factory http://acm.hdu.edu.cn/showproblem.php?pid=5536 Time Limit: 18000/9000 MS (Java/Others) ...
- hdu 5536 Chip Factory (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...
- HDU 5536 Chip Factory
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
随机推荐
- Django的学习需要掌握的一些基础和初步搭建自己的框架
一.Django的学习需要掌握的一些基础 第一个需要注意的点:客户端发送过来的数据结构组成: 第二个需要注意的点:动态网页和静态网页 静态网页:用户发送请求,服务端找到对应的静态文件返回给浏览器,静态 ...
- leetcode166 Fraction to Recurring Decimal
思路: 模拟. 实现: class Solution { public: string fractionToDecimal(int numerator, int denominator) { long ...
- [Luogu1343]地震逃生 最大流
题目链接:https://www.luogu.org/problem/show?pid=1343 dinic跑最大流. #include<cstdio> #include<cstri ...
- Hibernate三种批量处理数据
概念:批量处理数据是指在一个事务场景中处理大量数据. 在应用程序中难以避免进行批量操作,Hibernate提供了以下方式进行批量处理数据: (1)使用HQL进行批量操作 数据库层面 execute ...
- GetClassName 取得的类名有
今天上午稍微跟踪了一下自己的项目里面的各个空间,得知GetClassName可以取到以下类名:Static\Edit\Button\ComboBox\msctls_trackbar32\SysTabC ...
- Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits i
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- Android的开机流程及对应源码位置分析
1.系统引导bootloader 1)源码:bootable/bootloader/* 2)说明:加电后,CPU将先执行bootloader程序,此处有三种选择 a)开机按Camera+Power启动 ...
- Android笔记--Bitmap
Android | Bitmap解析 Android中Bitmap是对图像的一种抽象.通过他可以对相应的图像进行剪裁,旋转,压缩,缩放等操作.这里循序渐进的一步步了解Bitmap的相关内容. 先了解B ...
- log4sql介绍
log4sql介绍log4j环境中简单配置的情况下可收集执行的SQL语句和JDBC执行情况,如预编译的”?“显示成参数的实际值 下载log4sql.jar第一步:http://log4sql.sour ...
- MySQL 导出一句话
听说是很老的东西了,学习的时候发现还是很好用的,故学习转载过来,留备学习. mysql 导出一句话 方法1:网上流行的方法 流程:(1)建表--->(2)插入数据--->(3)select ...