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. tar 只解压tar包中某个文件

    sh-4.1# ls test.tar sh-4.1# tar -tf test.tar ./ecs20161207.png ./ecs.png ./ecs.xml ./rds.png ./Scree ...

  2. 列出zip文件内全部内容 当前目录下的所有文件压缩成zip格式的文件(file.zip)

    [root@ok Desktop]# zip -r image.zip ./*.jpg adding: 20161007_113743.jpg (deflated 0%) adding: 201610 ...

  3. shell学习三十四天----printf详解

    http://blog.csdn.net/shanyongxu/article/details/46744055

  4. ActiveMQ的几种集群配置

    ActiveMQ是一款功能强大的消息服务器,它支持许多种开发语言,例如Java, C, C++, C#等等.企业级消息服务器无论对服务器稳定性还是速度,要求都很高,而ActiveMQ的分布式集群则能很 ...

  5. Delphi中的各种字符串、String、PChar、Char数组

    参考博客:http://www.cnblogs.com/pchmonster/archive/2011/12/14/2287686.html 其中的所有代码均在Delphi7下测试通过. Delphi ...

  6. 玩转SSRS第九篇---匿名访问的一个间接方法

    SSRS是一个功能丰富的报表平台,我们可以在这个平台上实现各种不同需求的报表应用,所以这个平台也吸引了很多.net框架之外的技术,希望能在应用中引入SSRS的报表,比如JSP或者PHP页面,这个时候系 ...

  7. C# Func<T,TResult>

    using System; namespace FuncDemo { internal class Program { private static void Main() { //类似委托功能 Fu ...

  8. sublime text 2 安装emmet插件

    一.添加插件之前先 下载Package Control 按 Ctrl+`(就是~这个键) 复制下面的代码 确认 重新启动sublime text2 import urllib2,os;pf='Pack ...

  9. hdu 4273 2012长春赛区网络赛 三维凸包中心到最近面距离 ***

    新模板 /* HDU 4273 Rescue 给一个三维凸包,求重心到表面的最短距离 模板题:三维凸包+多边形重心+点面距离 */ #include<stdio.h> #include&l ...

  10. 注解:【无连接表的】Hibernate单向N->1关联

    Person与Address关联:单向N->1,[无连接表的] Person.java package org.crazyit.app.domain; import javax.persiste ...