HDU-5536 Chip Factory (字典树)
题目大意:给n个数,编号为1~n,取三个编号不同的数,使表达式(a+b)^c的值最大。
题目分析:将这n个数按二进制位建立一棵trie。枚举i、j的和,查询亦或最大值,但在查询之前要把i、j在trie中删除,查询完毕后再插入trie。
ps:用数组实现trie会超时,因为每次test case之前都要初始化数组,非常耗时。
代码如下:
# include<iostream>
# include<cstdio>
# include<cmath>
# include<vector>
# include<list>
# include<queue>
# include<map>
# include<set>
# include<cstring>
# include<algorithm>
using namespace std;
# define LL long long const int N=1000;
const int INF=1000000000;
const double inf=1e20; struct Node
{
int cnt;
Node *son[2];
Node(){
cnt=0;
son[0]=son[1]=NULL;
}
};
Node *root;
int a[N+5];
int n; void update(int x)
{
Node *p=root;
for(int i=30;i>=0;--i){
int k=(x&(1<<i))>0?1:0;
if(p->son[k]==NULL)
p->son[k]=new Node();
++(p->son[k]->cnt);
p=p->son[k];
}
} void erase(LL x)
{
Node *p=root;
for(int i=30;i>=0;--i){
int k=(x&(1<<i))>0?1:0;
--(p->son[k]->cnt);
p=p->son[k];
}
} int query(int x)
{
int res=0;
Node *p=root;
for(int i=30;i>=0;--i){
int k=(x&(1<<i))>0?1:0;
if(p->son[k^1]!=NULL&&p->son[k^1]->cnt>0){
res|=(1<<i);
p=p->son[k^1];
}else{
p=p->son[k];
}
}
return res;
} void delTree(Node *p)
{
if(p==NULL) return ;
delTree(p->son[0]);
delTree(p->son[1]);
delete p;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
root=new Node();
for(int i=0;i<n;++i){
scanf("%d",a+i);
update(a[i]);
}
int ans=0;
for(int i=0;i<n;++i){
erase(a[i]);
for(int j=i+1;j<n;++j){
erase(a[j]);
ans=max(ans,query(a[i]+a[j]));
update(a[j]);
}
update(a[i]);
}
printf("%d\n",ans);
delTree(root);
}
return 0;
}
HDU-5536 Chip Factory (字典树)的更多相关文章
- HDU 5536 Chip Factory 字典树+贪心
给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...
- 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 字典树+bitset 铜牌题
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- HDU 5536 Chip Factory 【01字典树删除】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536 Chip Factory Time Limit: 18000/9000 MS (Java/Ot ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- HDU 5536 Chip Factory (暴力+01字典树)
<题目链接> 题目大意: 给定一个数字序列,让你从中找出三个不同的数,从而求出:$\max_{i,j,k} (s_i+s_j) \oplus s_k$的值. 解题分析:先建好01字典树,然 ...
- 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 (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...
- HDU 5536 Chip Factory
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- 2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
随机推荐
- jQuery 操作CSS
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- hdu1394
//Accepted 292 KB 46 ms //利用线段树求逆序数 //对于每个数看前面比他大的数有多少个,更新这个数的个数 #include <cstdio> #include &l ...
- git vs svn
http://www.tuicool.com/articles/e2MnAb Git与SVN的不同之处 svn为集中化的版本控制,svn获取最新的版本或者提交更新,历史记录等信息每次都要连接中央版本库 ...
- hdoj-2025
#include "stdio.h"#include "string.h"void sort(char ch[],int count[],int n,int f ...
- Android与服务器http连接模块代码
package com.example.httpdemo2; import java.io.BufferedReader; import java.io.IOException; import jav ...
- c++回调函数 callback
C++中实现回调机制的几种方式 (1)Callback方式Callback的本质是设置一个函数指针进去,然后在需要需要触发某个事件时调用该方法, 比如Windows的窗口消息处理函数就是这种类型.比如 ...
- swift系统学习第三章
第九节:结构体-sturt //: Playground - noun: a place where people can play import UIKit /* swift学习第九节 结构体:st ...
- 如何运用inno在安装和卸载时提示用户结束进程?
我尝试着写了一段,但是卸载段存在问题,请指点! CODE [Files]Source: ISTask.dll; DestDir: {app}; Flags: ignoreversion [Code]f ...
- pscp详解
pscp详解 在linux中,我们常用scp命令传输文件: 如以下实例,我们想把当前服务器文件abc.sql传输到192.168.1.1服务器上,我们可以执行以下命令: scp /home/perso ...
- 使用.bat 文件,批量编译项目文件。
使用.bat 文件,批量编译项目文件. 2008-6-1来源:www.aspcool.com 作者:PCJIM 点击:次 path %path%;D:\Program Files\Microsof ...