传送门:>Here<

题意:给出一个长度为N的序列,求$Max\{ (a_i + a_j) ⊕ a_k \}$ (i,j,k均不相同)  ($N \leq 1000$)

解题思路

  既然$O(n^3)$不行,就考虑$O(n^2 \ log \ n)$的做法。

  网上说得很对,凡是和xor有关的80%都是Trie……

  将所有数的二进制建立Trie树,枚举$i,j$——此时在trie树中删去$a_i, a_j$,然后用上一篇文章的方法求得最大的异或。

  那么这道题的关键问题就在于如何删去$a_i, a_j$?每次重新建树显然又$O(n^3)$了(还不止)。

  考虑维护一个cnt数组,代表每个节点出现的次数。每一次删去的时候就像插入一样走一遍把对应节点的cnt减掉,然后在query的时候判定只能访问cnt>0的。这样很方便地处理好了问题

Code

  数组要清零

/*By DennyQi*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
const int MAXN = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) +(x << ) + c - '', c = getchar(); return x * w;
}
int T,N;
int a[],ch[MAXN][],cnt[MAXN],End[MAXN],num_node;
bool b[];
inline void Convert(int x){
memset(b, , sizeof(b));
for(int i = ; x > ; --i){
b[i] = x%;
x >>= ;
}
}
inline void Insert(int x){
Convert(x);
int u=;
for(int i = ; i <= ; ++i){
if(!ch[u][b[i]]){
ch[u][b[i]] = ++num_node;
}
u = ch[u][b[i]];
++cnt[u];
}
End[u] = x;
}
inline void Clear(int x){
Convert(x);
int u=;
for(int i = ; i <= ; ++i){
u = ch[u][b[i]];
--cnt[u];
}
}
inline void Add(int x){
Convert(x);
int u=;
for(int i = ; i <= ; ++i){
u = ch[u][b[i]];
++cnt[u];
}
}
inline int Query(int k){
Convert(k);
int u = ;
for(int i = ; i <= ; ++i){
if(!cnt[ch[u][!b[i]]]){
u = ch[u][b[i]];
}
else{
u = ch[u][!b[i]];
}
}
return (k^End[u]);
}
inline void Init(){
memset(ch,,sizeof(ch));
memset(cnt,,sizeof(cnt));
memset(End,,sizeof(End));
num_node = ;
}
int main(){
T=r;
while(T--){
Init();
N=r;
for(int i = ; i <= N; ++i){
a[i]=r;
Insert(a[i]);
}
int ans = -;
for(int i = ; i < N; ++i){
for(int j = i+; j <= N; ++j){
Clear(a[i]);
Clear(a[j]);
ans = Max(ans, Query(a[i]+a[j]));
Add(a[i]);
Add(a[j]);
}
}
printf("%d\n", ans);
}
return ;
}

[HDU5536] Chip Factory的更多相关文章

  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 Chip Factory,又见字典树,好题+1!

    Chip Factory 题意:一个n个数的数列,求三个数其中两个数的和与另外一个数的异或值最大,输出这个最大值. 思路:和前面那个百度之星资格赛HDU4825的类似,多了两个过程,一个是枚举和,另一 ...

  3. [HDU-5536] Chip Factory (01字典树)

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

  4. HDU-5536 Chip Factory (字典树)

    题目大意:给n个数,编号为1~n,取三个编号不同的数,使表达式(a+b)^c的值最大. 题目分析:将这n个数按二进制位建立一棵trie.枚举i.j的和,查询亦或最大值,但在查询之前要把i.j在trie ...

  5. 【01字典树】hdu-5536 Chip Factory

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5536 [题意] 求一个式子,给出一组数,其中拿出ai,aj,ak三个数,使得Max{ (ai+aj ...

  6. hdu5269 Chip Factory

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5536 题目: Chip Factory Time Limit: 18000/9000 MS ( ...

  7. 2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory

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

  8. HDU 5536 Chip Factory 字典树

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

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

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

随机推荐

  1. 朱晔和你聊Spring系列S1E9:聊聊Spring的那些注解

    本文我们来梳理一下Spring的那些注解,如下图所示,大概从几方面列出了Spring的一些注解: 如果此图看不清楚也没事,请运行下面的代码输出所有的结果. Spring目前的趋势是使用注解结合Java ...

  2. ASP.NET MVC5+EF6+EasyUI 后台管理系统-WebApi的用法与调试

    1:ASP.NET MVC5+EF6+EasyUI 后台管理系统(1)-WebApi与Unity注入 使用Unity是为了使用我们后台的BLL和DAL层 2:ASP.NET MVC5+EF6+Easy ...

  3. .net Core 调用微信Jsapi接口,H5解析二维码

    项目里需要用到扫描二维码,自己实现,不会. 找到了两种解决方案: 通过reqrcode.js,这是一个前端解析二维码内容的js库.如果二维码比较清晰,用这种效果也不错 调用微信扫一扫功能,这种效果很好 ...

  4. struts2的基本使用

    struts2在web中当作前端控制器,接收来自页面的请求,使用过滤器拦截模式对请求进行拦截并交给相应配置的action类处理. 所以在web中使用最重要的是struts2的核心过滤器StrutsPr ...

  5. Python-类的特性(property)

    什么是特性property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 例一:BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性而非方法,如果我们将其做成一个 ...

  6. 助教总结 -【福大软工实践-2017-2018-K班】

    助教总结 -[福大软工实践-2017-2018-K班] 非常抱歉这么晚才来写总结! 助教工作 助教共发表博客39篇. 助教共点评约500条. 起步 对于常规课程的起步,通常都是在第一次课堂上由老师对课 ...

  7. InvalidDataAccessResourceUsageException:mysql保留字引发的血案

    org.springframework.dao.InvalidDataAccessResourceUsageException: could NOT EXECUTE statement; SQL [n ...

  8. iOS蓝牙开发之iBeacon技术

    iBeacon组成信息: 1 .UUID(universally unique identifier):一个128位的唯一标识一个或多个Beacon基站为特定类型或特定的组织. 2. Major:一个 ...

  9. [转帖]SAP一句话入门:Sales and Distribution

    SAP一句话入门:Sales and Distribution http://blog.vsharing.com/MilesForce/A616565.html SD是Sales and Distri ...

  10. 428.x的n次幂

    实现 pow(x,n) 不用担心精度,当答案和标准输出差绝对值小于1e-3时都算正确 样例 Pow(2.1, 3) = 9.261 Pow(0, 1) = 0 Pow(1, 0) = 1 挑战 O(l ...