ACM: Find MaxXorSum 解题报告-字典树
Find MaxXorSum
Time Limit:2000MS Memory Limit:65535KB 64bit IO Format: Description
Given n non-negative integers, you need to find two integers a and b that a xor b is maximum. xor is exclusive-or.
Input
Input starts with an integer T(T <= ) denoting the number of tests.
For each test case, the first line contains an integer n(n <= ), the next line contains a1, a2, a3, ......, an( <= ai <= );
Output
For each test case, print you answer.
Sample Input 5 Sample Output 11 这个题目其实思路很简单,就我知道的写法有Trie树指针和静态数组两种写法,一开始写的指针用了pow函数TLE了2次,然后换位运算又TLE两次醉了。。
后面换成静态数组数组开大了RE了一次。。。哭。。。
这是AC代码:
#include"iostream"
#include"algorithm"
#include"cstdio"
#include"cmath"
#include"cstring"
#define MX 1400000
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int tree[MX][],xx; void BuildTrie(long long a) {
int i=;
int p=;
while(<=i) {
bool num=a&(<<i);
if(!tree[p][num]) { //如果节点为空
tree[p][num]=++xx;//标记并创建新的子节点
}
p=tree[p][num];
// cout<<"YES"<<i<<" B is:"<<num<<"\n"; //建立的Trie树的样子
i--;
}
} long long Query(long long a) {
int i=,p=;
long long ans=;
while(<=i) {
bool num=!(a&(<<i)); //取反查找
if(tree[p][num]) p=tree[p][num],ans+=<<i;//如果和原来的那一为相反的存在的话,返回值就加上,并且在这个支路走
else p = tree[p][!num]; //按照相同的顺序找
// cout<<"YES"<<i<<" B is:"<<num<<" ans is:"<<ans<<endl; //查询时候的Trie树的样子
i--;
}
return ans;
} int main() {
int T,n;
long long a[],ans;
scanf("%d",&T);
while(T--) {
ans=;
memset(tree,,sizeof(tree));
xx=;
scanf("%d",&n);
for(int i=; i<=n; i++) {
scanf("%I64d",&a[i]);
BuildTrie(a[i]);
}
//cout<<"YES1\n";
for(int i=; i<=n; i++) {
ans=max(ans,Query(a[i]));
}
// cout<<"YES2\n";
printf("%I64d\n",ans);
}
return ;
}
这是指针的写法,感觉复杂度和上面的没啥区别,为啥就TLE了呢? 希望有人知道给我指点下。
#include"iostream"
#include"algorithm"
#include"cstdio"
#include"cmath"
#include"cstring"
#define MX 110000
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std; struct Trie {
Trie *next[];
} root; void BuildTrie(int a) {
Trie *p=&root,*q;
int i=;
while(i>=) {
bool num=a&(<<i);
if(p->next[num]==NULL) {
q=(Trie *)malloc(sizeof(root));//申请一块新内存; //动态分配内存
q->next[]=NULL;
q->next[]=NULL; //清空申请内存的所有子节点
p->next[num]=q; //往子节点下去继续记录字典树
p=p->next[num];
} else {
p=p->next[num];
}
// cout<<"YES"<<i<<" B is:"<<num<<"\n"; //建立的Trie树的样子
i--;
}
} long long Query(int a) {
Trie *p=&root;
long long ans=;
int i=;
while(<=i) {
bool num=a&(<<i);
if(p->next[!num]!=NULL) p=p->next[!num],ans+=<<(i);//如果和原来的那一为相反的存在的话,返回值就加上,并且在这个支路走
else if(p->next[num]!=NULL) p=p->next[num]; //按照相同的顺序找
// cout<<"YES"<<i<<" B is:"<<num<<"\n"<<"ans is:"<<ans<<endl; //查询时候的Trie树的样子
i--;
}
return ans;
} int main() {
int T,n,a[MX];
long long ans;
scanf("%d",&T);
while(T--) {
ans=;
root.next[]=NULL;
root.next[]=NULL;
scanf("%d",&n);
for(int i=; i<n; i++) {
scanf("%d",&a[i]);
BuildTrie(a[i]);
}
//cout<<"YES1\n";
for(int i=; i<n; i++) {
ans=max(ans,Query(a[i]));
}
// cout<<"YES2\n";
printf("%I64d\n",ans);
}
return ;
}
ACM: Find MaxXorSum 解题报告-字典树的更多相关文章
- ACM:统计难题 解题报告-字典树(Trie树)
统计难题 Time Limit:2000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Submit Status ...
- ACM: Just a Hook 解题报告 -线段树
E - Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- [ACM] hdu 1671 Phone List (字典树)
Phone List Problem Description Given a list of phone numbers, determine if it is consistent in the s ...
- ACM: Billboard 解题报告-线段树
Billboard Time Limit:8000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descript ...
- ACM Minimum Inversion Number 解题报告 -线段树
C - Minimum Inversion Number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- ACM: Hotel 解题报告 - 线段树-区间合并
Hotel Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description The ...
- ACM: 敌兵布阵 解题报告 -线段树
敌兵布阵 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description Li ...
- ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
- ACM: I Hate It 解题报告 - 线段树
I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...
随机推荐
- Shell编程基础教程2--变量和运算符
2.变量和运算符 2.1.变量的类型 本地变量:环境变量:变量替换(显示变量):位置变量:标准变量:特殊变量: 2.2.本地变量 本地变量在用户现在的shell生命周期的脚本中使用 在命令行, LOC ...
- Newtonsoft.Json(Json.Net)学习笔记(转)
概述 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库,通过Nuget获取.(查看原文) 下面是Json序列化和反序列化的简单封装: /// <summary&g ...
- 那些年,我们在Django web开发中踩过的坑(一)——神奇的‘/’与ajax+iframe上传
一.上传图片并在前端展示 为了避免前端整体刷新,我们采用ajax+iframe(兼容所有浏览器)上传,这样用户上传之后就可以立即看到图片: 上传前: 上传后: 前端部分html: <form s ...
- MYSQL建表语法(主键,外键,联合主键)
在看<Learning SQL>第二版, 慢慢打实SQL的基础. 建表一: ), lname ), gender ENUM(), city ), state ), country ), p ...
- penghui_031413 Bat命令学习
penghui_031413 Bat命令学习 基础部分:====================================================================== ...
- .NET NLog 详解(一)
安装NLog NLog 的源代码托管在Github 上,一般的人直接使用NuGet就可以了. 这里我们选择安装NLog.Config.当然最方便的还是直接使用命令行: Install-Package ...
- 6-01T-SQL中的运算符
算术运算符:+.-.*./.%. 赋值运算符:= 逻辑运算符:AND.OR.NOT. 比较运算符:>,<,<=,>=,<>.=,!=. 连接运算符:"+& ...
- PHPCMS 实现上一篇、下一篇
方法一:直接调用phpcms系统的函数 <div class="info"> <span>上一篇:<a href="{$previous_p ...
- Macbook Pro安装win7
1.进入OS X系统,在实用工具中打开Boot Camp助理 2.用磁盘工具对磁盘进行分区,将需要安装win7的分区格式化成FAT格式 3.用Boot Camp对磁盘进行分割,然后插入win7的安装光 ...
- linux文本模式下使用PPPOE拨号ADSL上网的方法
转自:http://www.myzhenai.com.cn/post/945.html 转载请注明出处:http://www.myzhenai.com/thread-15431-1-1.html ht ...