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 ...
随机推荐
- Spring.Net的IOC入门
1.构造器注入 namespace Spring.Net { class Program { //构造器注入 static void Main(string[] args) { IApplicatio ...
- .netWeb方向:语言+技术
常用语言+技术 C# T-Sql ADO.NEt JavaScript Asp.Net MVC HTML CSS DOM AJAX Entity Framework Regular expressio ...
- Asp.net窄屏页面 手机端新闻列表
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SearchNotice.a ...
- Pyqt QListWidget之缩略图列表
QListWidget 可以设置模型setViewMode 当setViewMode值为QListView.IconMode 表示Icon模式 以下代码来自Pyqt Example #!/usr/b ...
- 解决Pyqt打包后运行报错:应用程序无法启动 因为程序的并行配置不正确
做了一个生成二维码的小程序:http://www.cnblogs.com/dcb3688/p/4241048.html 直接运行脚本没问题,用pyinstaller打包后再运行就直接报错了: 应用程序 ...
- SSIS 包单元测试检查列表
1. 使用脚本任务(Script tasks) 组建的时候,在日志里增加一些调试信息,例如变量更新信息,可以帮助我们从日志中查看到变量是在何时何地更新的. 2. 使用ForceExecutionRes ...
- STL Map的使用
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力.下面就通过示例记录一下map的使用: 一.向map中 ...
- CentOS 6.5下samba服务器搭建与配置
转自:http://www.centoscn.com/CentosServer/ftp/2014/1023/3989.html 写在前面: 首先关闭防火墙:service iptables stop, ...
- Unity3D项目开发一点经验
我们主要使用3dsmax2010进行制作,输出FBX的类型导入Unity3D中.默认情况下,3dsmax8可以和U3D软件直接融合,自动转换为FBX物体. 注意事项如下: 1.面数控制 在MAX软件中 ...
- ZEALER背后的乐视云视频
ZEALER是我非常喜欢的一个测评网站,经常访问看看手机.电动牙刷及机械键盘的测试视频,非常欣赏王自如的数据化测评理念.敬畏之心,以及不祛痘的视频. 刚好最近对网络视频应用比较感兴趣,觉得ZEALER ...