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!的更多相关文章

  1. HDU 5536 Chip Factory 【01字典树删除】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536 Chip Factory Time Limit: 18000/9000 MS (Java/Ot ...

  2. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

  3. HDU 5536 Chip Factory (暴力+01字典树)

    <题目链接> 题目大意: 给定一个数字序列,让你从中找出三个不同的数,从而求出:$\max_{i,j,k} (s_i+s_j) \oplus s_k$的值. 解题分析:先建好01字典树,然 ...

  4. HDU 5536 Chip Factory 字典树+贪心

    给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...

  5. HDU 5536 Chip Factory 字典树

    Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  6. hdu 5536 Chip Factory 字典树+bitset 铜牌题

    Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  7. Chip Factory(01字典树)

    Chip Factory http://acm.hdu.edu.cn/showproblem.php?pid=5536 Time Limit: 18000/9000 MS (Java/Others)  ...

  8. hdu 5536 Chip Factory (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...

  9. HDU 5536 Chip Factory

    Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

随机推荐

  1. 五、UML类图和六大原则-----《大话设计模式》

    一.单一职责原则     就一个类而言,应该仅有一个引起它变化的原因.     如果一个类承担的职责过多,就等于把这些职责耦合在一起,一个职责的变化可能会削弱或者抑制这个类完成其他职责的能力.这种耦合 ...

  2. Android adb命令,linux中各种命令

    常用的ADB命令 1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器 ...

  3. moment.js获取当前日期是当年的第几周

    /** * 实现当前日期是当年的第几周,再向前和向后推几周 * js数组保存当前日期的前后两周(共五周的数据) * */ var initSearchMajorChanges = function() ...

  4. 【Web应用-Kudu】Kudu 管理和诊断 azure web 应用

    Azure  Kudu是 GitHub 上的一个开源项目,Kudu 站点 (也称为网站控制管理 SCM) 提供了一系列的在线工具,可以帮助用户查看 web 应用的设置,诊断 web 应用,以及安装 w ...

  5. 1898 ERROR nova.compute.manager

    2018-06-10 21:03:54.045 1898 ERROR nova.compute.manager [instance: 15a6c26f-b8af-4a3e-a3df-8552c16e0 ...

  6. scrollviews page分页实现方式

    代码 buttonX = 0; buttonW = 50; buttonH = 20; margin = (self.view.width - 5 * buttonW) / 6; CGFloat ym ...

  7. 稳定性 耗时 gc 过长问题排查 和工具

    自己的另外一篇: http://www.cnblogs.com/fei33423/p/7805186.html 偶有耗时抖动? gc 也有长耗时? fullgc 也是? 有同学反馈 swap 可能导致 ...

  8. 使用Microsoft Hadoop(一)

    To run this program, stage some data in HDFS: 1. create a text file called input.txt that has one in ...

  9. java设计模式基础 - 解决某一类问题最行之有效的方法,框架是大的设计模式.

    一.单例模式(Singleton) 1.单例对象(Singleton)是一种常用的设计模式.在Java应用中,单例对象能保证在一个JVM中,该对象只有一个实例存在.这样的模式有几个好处: 1>某 ...

  10. egg.js 学习之 中间件使用

    1.在框架和插件中使用中间件 编写中间件 我们先来通过编写一个简单的中间件,来看看中间件的写法. // app/middleware/middlewareOne.js // app/middlewar ...