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 解题报告-字典树的更多相关文章

  1. ACM:统计难题 解题报告-字典树(Trie树)

    统计难题 Time Limit:2000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status ...

  2. ACM: Just a Hook 解题报告 -线段树

    E - Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   D ...

  3. [ACM] hdu 1671 Phone List (字典树)

    Phone List Problem Description Given a list of phone numbers, determine if it is consistent in the s ...

  4. ACM: Billboard 解题报告-线段树

     Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descript ...

  5. ACM Minimum Inversion Number 解题报告 -线段树

    C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  6. ACM: Hotel 解题报告 - 线段树-区间合并

    Hotel Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description The ...

  7. ACM: 敌兵布阵 解题报告 -线段树

    敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Li ...

  8. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

  9. ACM: I Hate It 解题报告 - 线段树

    I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...

随机推荐

  1. Jquery.Datatables td宽度太长的情况下,自动换行

    在 td 里面 加上 style="word-wrap:break-word;" 自动换行就好了,如果不想换行,可以将超出内容设为隐藏, overflow:hidden; whit ...

  2. Asp.Net - 9.socket(聊天室)

    9.1 Socket相关概念 IP地址 每台联网的电脑都有一个唯一的IP地址. 长度32位,分为四段,每段8位,用十进制数字表示,每段范围 0 ~ 255 特殊IP:127.0.0.1 用户本地网卡测 ...

  3. Python 与 C# lambda表达式比较

    Python里到lambda表达式非常简约, lam =lambda a: a*2 --> lam(3) 6 在某些情况下确实挺好用到.但是相比C#到lambda表达式,还是不够强大(我不是在黑 ...

  4. Delphi面向对象编程

    一.面向对象介绍 OOP是使用独立的对象(包含数据和代码)作为应用程序模块的范例.虽然OOP不能使得代码容易编写,但是它能够使得代码易于维护.将数据和代码结合在一起,能够使定位和修复错误的工作简单化, ...

  5. Delphi中的基础数据类型

    参考http://www.cnblogs.com/del/archive/2007/12/04/982167.html 在学习之初,在这么多的数据类型中,最好记住这五种标准数据类型(整型.实型.字符型 ...

  6. 那些年,我们在Django web开发中踩过的坑(一)——神奇的‘/’与ajax+iframe上传

    一.上传图片并在前端展示 为了避免前端整体刷新,我们采用ajax+iframe(兼容所有浏览器)上传,这样用户上传之后就可以立即看到图片: 上传前: 上传后: 前端部分html: <form s ...

  7. 无废话ExtJs 入门教程三[窗体:Window组件]

    无废话ExtJs 入门教程三[窗体:Window组件] extjs技术交流,欢迎加群(201926085) 1.代码如下: 1 <!DOCTYPE html PUBLIC "-//W3 ...

  8. 运维自动化之ansible的安装与使用(包括模块与playbook使用)(转发)

    原文  http://dl528888.blog.51cto.com/2382721/1435415 我使用过puppet(地址是http://dl528888.blog.51cto.com/2382 ...

  9. wp8 入门到精通 MultiMsgPrompt

    List<NotifyMsg> arraymsg = new List<NotifyMsg>(); List<NotifyInfo> ArrayNotifyInfo ...

  10. php 以图搜图

    感知哈希算法count < =5 匹配最相似count > 10 两张不同的图片var_dump(ImageHash::run('1.jpg’, '2.jpg’)); <?php c ...