hdu5536 字典树xor
一想到xor还要求最大类似的题,字典树效率高。
此代码c++ TLE。
- #include<stdio.h>
- #include<string.h>
- const int maxn = ;
- struct node
- {
- int num;
- node *next[];
- void init()
- {
- next[]=next[]=NULL;
- num=;
- }
- };
- node *trie,*p=NULL;
- int a[maxn],b[maxn],n;
- int max(int x,int y)
- {
- return x>y?x:y;
- }
- void init()
- {
- trie=new node;
- trie->init();
- }
- void add(int x)
- {
- p=trie;
- int i,j;
- for(i=;i>=;i--)
- {
- int t;
- if(x&(<<i))
- t=;
- else t=;
- if(t)
- {
- if(p->next[t]==NULL)
- {
- node *q=new node;
- q->init();
- p->next[t]=q;
- }
- p=p->next[t];
- p->num++;
- }
- else
- {
- if(p->next[t]==NULL)
- {
- node *q=new node;
- q->init();
- p->next[t]=q;
- }
- p=p->next[t];
- p->num++;
- }
- }
- }
- void Del(int x)
- {
- p=trie;
- int i;
- for(i=;i>=;i--)
- {
- int t;
- if(x&(<<i))
- t=;
- else t=;
- p=p->next[t];
- p->num--;
- }
- }
- int query(int x)
- {
- p=trie;
- int fx=;
- for(int i=;i>=;i--)
- {
- int t;
- if(x&(<<i))t=;
- else t=;
- if(t)
- {
- if(p->next[]!=NULL&&p->next[]->num!=)
- {
- p=p->next[];
- }
- else
- {
- fx=fx^(<<i);
- p=p->next[];
- }
- }
- else
- {
- if(p->next[]!=NULL&&p->next[]->num!=)
- {
- fx=fx^(<<i);
- p=p->next[];
- }
- else p=p->next[];
- }
- }
- return fx;
- }
- void Del(node *trie)
- {
- for(int i=;i<=;i++)
- {
- if(trie->next[i])
- Del(trie->next[i]);
- }
- delete trie;
- }
- int main()
- {
- int i,j,t;
- scanf("%d",&t);
- while(t--)
- {
- init();
- scanf("%d",&n);
- for(i=;i<n;i++)
- {
- scanf("%d",&a[i]);
- add(a[i]);
- }
- int ans=;
- for(i=;i<n;i++)
- {
- for(j=;j<n;j++)
- {
- if(i==j)
- continue;
- Del(a[i]);
- Del(a[j]);
- int fp=a[i]+a[j];
- int ret=query(fp);
- //printf("%d ",ret);
- ans=max(ans,fp^ret);
- add(a[i]);
- add(a[j]);
- }
- }
- printf("%d\n",ans);
- Del(trie);
- }
- }
hdu5536 字典树xor的更多相关文章
- hdu4825 字典树 XOR
用字典树思想来做.对于一个数,给出他的二进制,然后更具二进制建立字典树,然后每次询问的时候的数也给出二进制,如果当前为1,那就向0走,为0,向1走. #include<stdio.h> # ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- 字典树-百度之星-Xor Sum
Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...
- HDU 4825 Xor Sum 字典树+位运算
点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- 2014百度之星第三题Xor Sum(字典树+异或运算)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- HDU--5269 ZYB loves Xor I (字典树)
题目电波: HDU--5269 ZYB loves Xor I 首先我们先解决 ai xor aj 每个数转化为二进制 我们用字典树统计 每个节点 0 和 1 的出现的个数 #include< ...
- HDU--4825 Xor Sum (字典树)
题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...
- 51nod 1295 XOR key 可持久化01字典树
题意 给出一个长度为\(n\)的正整数数组\(a\),再给出\(q\)个询问,每次询问给出3个数,\(L,R,X(L<=R)\).求\(a[L]\)至\(a[R]\)这\(R-L+1\)个数中, ...
- CH 1602 - The XOR Largest Pair - [字典树变形]
题目链接:传送门 描述在给定的 $N$ 个整数 $A_1, A_2,\cdots,A_N$ 中选出两个进行xor运算,得到的结果最大是多少? 输入格式第一行一个整数 $N$,第二行 $N$ 个整数 $ ...
随机推荐
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- codeforces 709A A. Juicer(水题)
题目链接: A. Juicer 题意: 给出n个橘子,汁漫出来了就倒出来,反正就是要求要倒几次; 思路: AC代码: #include <iostream> #include <cs ...
- 使用Unity开发Android的几种调试方法
前言 本文举例几种Android 调试的方法(PS:我是通过unity引擎来开发安卓游戏) Eclipse + adt 查看LOG 1.为Eclipse 装上adt 插件 2.打开Eclipse 的L ...
- php strcmp引起的问题
在官方的文档有这么一端说明: Note a difference between 5.2 and 5.3 versions echo (int)strcmp('pending',array()); w ...
- 使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
调用jQuery的ajax方法时,jQuery会根据post或者get协议对参数data进行序列化; 如果提交的数据使用复杂的json数据,例如: {userId:32323,userName:{fi ...
- Converting a Polygon ZM shape file to a regular Shape Polygon
from:http://blog.csdn.net/qb371/article/details/8102109 Locate the following tool - ArcToolbox > ...
- Adivisor
1.Adivisor是一种特殊的Aspect,Advisor代表spring中的Aspect 2.区别:advisor只持有一个Pointcut和一个advice,而aspect可以多个pointcu ...
- .net 4.0 自定义本地缓存策略的不同实现
在分布式系统的开发中,为了提高系统运行性能,我们从服务器中获取的数据需要缓存在本地,以便下次使用,而不用从服务器中重复获取,有同学可能要问,为什么不使用 分布式缓存等,注意,服务器端肯定是考虑到扩展, ...
- HMAC-MD5算法原理及实现
以下是分析节选,对于更详细的描述可以查阅RFC2104文档. HMAC需要一个加密用散列函数(表示为H)和一个密钥K. 假设H是一个将数据块用一个基本的迭代压缩函数来加密的散列函数. 用B来表 ...
- SilverLight自定义ImageButton
SilverLight中XAML的写法和WPF一样,但是发现在自定义按钮上,没有WPF来的容易,下面说说我制作SilverLight中的ImageButton的一些思路. 在SilverLight中, ...