传送门:>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. en

    发音,这个应该算是学习英语的头等大事,如果没有机会和条件练好发音,也可以先将就着,不过后面你就会感觉到你说的人家可能会听不懂,我自己也曾经深受其害. 基本常用单词积累(大概2000~4000左右的词汇 ...

  2. Python异常处理try except

    原文地址:https://www.cnblogs.com/init-life/p/9105546.html 异常处理try except 在Python中,异常处理,主要是try except语句,通 ...

  3. BZOJ1969 航线规划

    给定一个无向图,每次删除一条边,求每次有多少关键边.一条边是关键边,当且仅当从1到n的所有路径都包含这条边.所有时刻图都联通. 考虑倒着做.相当于给一棵树,每次加一条边,这样树上这条边的两个端点间的路 ...

  4. HDU - 1540 线段树的合并

    这个题题意我大概解释一下,就是一开始一条直线,上面的点全是联通的,有三种操作 1.操作D把从左往右第x个村庄摧毁,然后断开两边的联通. 2.询问Q节点相联通的最长长度 3.把最后破坏的村庄重建. 这个 ...

  5. 简约时尚商城wordpress主题-storefront

    wordpress主题:简约时尚商城主题-storefront 简简单的商城模板,挺适合一些懒人所用.后天功能也挺不错,希望大家喜欢. WooCommerce 集成 商城是基为用 WooCommerc ...

  6. Ajax中文乱码的解决

    网上有很多解决Ajax中文乱码的例子,昨晚弄了很久,最终确定一种“确实”有效地方法.首先我有必要说明一下我遇到的情况:有一个注册页面,注册用户填完信息并提交后,页面获得信息并通过java servle ...

  7. MyBatis使用注解开发

  8. 数组建 BST

    #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int N, root = 1; int ...

  9. How To: Capture Android & iOS Traffic with Fiddler

    How To: Capture iOS Traffic with Fiddlerhttps://www.telerik.com/blogs/how-to-capture-ios-traffic-wit ...

  10. PHP PSR代码规范

    转载: https://www.awaimai.com/916.html PSR是PHP通用性框架小组 (PHP Framework Interop Group) 制定的PHP代码编写格式规范,是PH ...