题意:给定n个数字,第i个数字为a[i],求max((a[i]+a[j])^a[k]),其中i,j,k互不相同

n<=1000,0<=a[i]<=1e9

思路:队友写的,抱大腿

先对于a[i]建立Trie树

枚举i和j后删除Trie中的i和j,然后就是经典的Trie树应用:优先朝与a[i]+a[j]的当前位不同的方向走,能获得最大xor和

 #include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1e3+;
int a[maxn*maxn][];
int b[maxn],num[maxn*maxn*],tot;
void init()
{
tot=;
}
void insert (int x)
{
int rt=;
for(int i=;i>=;i--)
{
int g=(x>>i)&;
if(a[rt][g]==)
{
a[rt][g]=++tot;
}
rt=a[rt][g];
num[rt]++;
}
}
void del(int x)
{
int rt=;
for(int i=;i>=;i--)
{
int g=(x>>i)&;
rt=a[rt][g];
num[rt]--;
}
}
int find(int x)
{
int rt=;
int sum=;
for(int i=;i>=;i--)
{
int g=(x>>i)&;
// printf("%d %d %d\n",g,a[rt][!g],a[rt][g]);
if(a[rt][!g]==||num[a[rt][!g]]==)
{
rt=a[rt][g];
}
else
{
// printf("i = %d\n",i);
rt=a[rt][!g];
sum+=<<i;
}
//printf("%d\n",sum);
}
return sum;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&b[i]);
insert(b[i]);
}
int ma=;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
{
del(b[i]);
del(b[j]);
// printf("%d\n",find(b[i]+b[j]));
// puts("sdadas");
ma=max(ma,find(b[i]+b[j]));
insert(b[i]);
insert(b[j]);
}
for(int i=;i<=tot;i++)
{
a[i][]=a[i][]=;
num[i]=;
}
printf("%d\n",ma);
}
}

【HDOJ5536】Chip Factory(Trie树)的更多相关文章

  1. hdu5536 Chip Factory 字典树+暴力 处理异或最大 令X=(a[i]+a[j])^a[k], i,j,k都不同。求最大的X。

    /** 题目:hdu5536 Chip Factory 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:给定n个数,令X=(a[i]+a[j] ...

  2. HDU 5536/ 2015长春区域 J.Chip Factory Trie

    Chip Factory Problem Description John is a manager of a CPU chip factory, the factory produces lots ...

  3. HDU 5536 Chip Factory 字典树

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

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

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

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

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

  6. HDU 5536 Chip Factory Trie

    题意: 给出\(n(3 \leq n \leq 1000)\)个数字,求\(max(s_i+s_j) \bigoplus s_k\),而且\(i,j,k\)互不相等. 分析: 把每个数字看成一个\(0 ...

  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字典树)

    Problem Description John is a manager of a CPU chip factory, the factory produces lots of chips ever ...

  9. hdu 5536 Chip Factory (01 Trie)

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

随机推荐

  1. 1269: [AHOI2006]文本编辑器editor

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5269  Solved: 2037[Submit][Status][Discuss] Descript ...

  2. [译]The Python Tutorial#7. Input and Output

    [译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...

  3. python3 练习题100例 (十)

    题目十:判断101-200之间有多少个素数,并输出所有素数. #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 题目十 ...

  4. python模块汇总练习

    模块练习 1.random模块 # print(random.random()) # print(random.randint(1,3)) #模拟随机验证码 def make_code(n=5): r ...

  5. D3DXCreateTexture

    HRESULT D3DXCreateTexture( __in LPDIRECT3DDEVICE9 pDevice, __in UINT Width, __in UINT Height, __in U ...

  6. 03014_EL技术

    1.EL表达式概述 EL(Express Language)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL出现的目的是要替代jsp页面中脚本的编写. 2.EL从域中取出数据(EL最重要的作 ...

  7. leetcode 【 Search for a Range 】python 实现

    题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...

  8. 使用android-junit-report.jar导出单元测试报告

    Android在使用脚本编译和测试时,使用默认的testrunner不会输出文件类型的单元测试报告,每次只能分析logcat的无法直观的看到单元测试结果和报告,这给编写自动化脚本带来了不少麻烦,虽然可 ...

  9. java自动化测试开发环境搭建(更新至2018年10月8日 11:42:15)

    1.安装JDK的1.8版本 官网下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 ...

  10. Python+Selenium练习篇之2-利用ID定位元素

    在前面一篇文章,我们介绍了如何摘取页面字段,通过正则进行匹配符合要求的字段.如果感觉有点困难,不能立马理解,没有关系.把字符串摘取放到第一篇,是因为自动化测试脚本,经常要利用字符串操作,字符串切割,查 ...