[HDU5536] Chip Factory
传送门:>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的更多相关文章
- 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] ...
- HDU-5536 Chip Factory,又见字典树,好题+1!
Chip Factory 题意:一个n个数的数列,求三个数其中两个数的和与另外一个数的异或值最大,输出这个最大值. 思路:和前面那个百度之星资格赛HDU4825的类似,多了两个过程,一个是枚举和,另一 ...
- [HDU-5536] Chip Factory (01字典树)
Problem Description John is a manager of a CPU chip factory, the factory produces lots of chips ever ...
- HDU-5536 Chip Factory (字典树)
题目大意:给n个数,编号为1~n,取三个编号不同的数,使表达式(a+b)^c的值最大. 题目分析:将这n个数按二进制位建立一棵trie.枚举i.j的和,查询亦或最大值,但在查询之前要把i.j在trie ...
- 【01字典树】hdu-5536 Chip Factory
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5536 [题意] 求一个式子,给出一组数,其中拿出ai,aj,ak三个数,使得Max{ (ai+aj ...
- hdu5269 Chip Factory
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5536 题目: Chip Factory Time Limit: 18000/9000 MS ( ...
- 2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- 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 字典树+贪心
给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...
随机推荐
- python_内置函数1_42
内置函数 内置函数大全: Built-in Functions abs() dict() help() min() setattr() all() dir() hex() next() ...
- POJ - 1177 线段树
POJ - 1177 扫描线 这道题也算是一道扫描线的经典题目了. 只不过这道题是算周长,非常有意思的一道题.我们已经知道了,一般求面积并,是如何求的,现在我们要把扫描线进行改造一下,使得能算周长. ...
- Javascript模板引擎handlebars使用
源地址:http://rfyiamcool.blog.51cto.com/1030776/1278620 代码示例: <!DOCTYPE html> <html> <he ...
- 关于 pip安装的可能错误的排除
今天安装selenium总是报错(下为错误信息) C:\Python27\Scripts>pip install seleniumCollecting seleniumC:\Python27\l ...
- 【kindle笔记】之 《鬼吹灯》-9-20
[kindle笔记]读书记录-总 9-20 日常吐槽 连着几天,基本是一口气读完了鬼吹灯. 想来,也算是阴差阳错了.本来是想看盗墓的,读了几页开头,心想坏了,拷贝错了,这是鬼吹灯-- 讲真的,每每读小 ...
- 【学习总结】之 3Blue1Brown系列
刷知乎看到的,各种力荐. YouTube: https://www.youtube.com/channel/UCYO_jab_esuFRV4b17AJtAw/featured B站: https:// ...
- Swagger UI 用法
Swagger - 简书https://www.jianshu.com/p/4115f2b53983 Swagger简介 - Ghost Stories - CSDN博客https://blog.cs ...
- Python3练习题 026:求100以内的素数
p = [i for i in range(2,100)] #建立2-99的列表 for i in range(3,100): #1和2都不用判断,从3开始 for j in range(2, ...
- [转帖]TLS 1.3 VS TLS 1.2,让你明白 TLS 1.3 的强大
TLS 1.3 VS TLS 1.2,让你明白 TLS 1.3 的强大 https://www.jianshu.com/p/efe44d4a7501?utm_source=oschina-app 又拍 ...
- taro 与uni-app对比
https://www.jianshu.com/p/03e08399587e (copy)