一想到xor还要求最大类似的题,字典树效率高。

此代码c++ TLE。

  1. #include<stdio.h>
  2. #include<string.h>
  3. const int maxn = ;
  4. struct node
  5. {
  6. int num;
  7. node *next[];
  8. void init()
  9. {
  10. next[]=next[]=NULL;
  11. num=;
  12. }
  13. };
  14. node *trie,*p=NULL;
  15. int a[maxn],b[maxn],n;
  16. int max(int x,int y)
  17. {
  18. return x>y?x:y;
  19. }
  20. void init()
  21. {
  22. trie=new node;
  23. trie->init();
  24. }
  25. void add(int x)
  26. {
  27. p=trie;
  28. int i,j;
  29. for(i=;i>=;i--)
  30. {
  31. int t;
  32. if(x&(<<i))
  33. t=;
  34. else t=;
  35. if(t)
  36. {
  37. if(p->next[t]==NULL)
  38. {
  39. node *q=new node;
  40. q->init();
  41. p->next[t]=q;
  42. }
  43. p=p->next[t];
  44. p->num++;
  45. }
  46. else
  47. {
  48. if(p->next[t]==NULL)
  49. {
  50. node *q=new node;
  51. q->init();
  52. p->next[t]=q;
  53. }
  54. p=p->next[t];
  55. p->num++;
  56. }
  57. }
  58. }
  59. void Del(int x)
  60. {
  61. p=trie;
  62. int i;
  63. for(i=;i>=;i--)
  64. {
  65. int t;
  66. if(x&(<<i))
  67. t=;
  68. else t=;
  69. p=p->next[t];
  70. p->num--;
  71. }
  72. }
  73. int query(int x)
  74. {
  75. p=trie;
  76. int fx=;
  77. for(int i=;i>=;i--)
  78. {
  79. int t;
  80. if(x&(<<i))t=;
  81. else t=;
  82. if(t)
  83. {
  84. if(p->next[]!=NULL&&p->next[]->num!=)
  85. {
  86. p=p->next[];
  87. }
  88. else
  89. {
  90. fx=fx^(<<i);
  91. p=p->next[];
  92. }
  93. }
  94. else
  95. {
  96. if(p->next[]!=NULL&&p->next[]->num!=)
  97. {
  98. fx=fx^(<<i);
  99. p=p->next[];
  100. }
  101. else p=p->next[];
  102. }
  103. }
  104. return fx;
  105. }
  106. void Del(node *trie)
  107. {
  108. for(int i=;i<=;i++)
  109. {
  110. if(trie->next[i])
  111. Del(trie->next[i]);
  112. }
  113. delete trie;
  114. }
  115. int main()
  116. {
  117. int i,j,t;
  118. scanf("%d",&t);
  119. while(t--)
  120. {
  121. init();
  122. scanf("%d",&n);
  123. for(i=;i<n;i++)
  124. {
  125. scanf("%d",&a[i]);
  126. add(a[i]);
  127. }
  128. int ans=;
  129. for(i=;i<n;i++)
  130. {
  131. for(j=;j<n;j++)
  132. {
  133. if(i==j)
  134. continue;
  135. Del(a[i]);
  136. Del(a[j]);
  137. int fp=a[i]+a[j];
  138. int ret=query(fp);
  139. //printf("%d ",ret);
  140. ans=max(ans,fp^ret);
  141. add(a[i]);
  142. add(a[j]);
  143. }
  144. }
  145. printf("%d\n",ans);
  146. Del(trie);
  147. }
  148. }

hdu5536 字典树xor的更多相关文章

  1. hdu4825 字典树 XOR

    用字典树思想来做.对于一个数,给出他的二进制,然后更具二进制建立字典树,然后每次询问的时候的数也给出二进制,如果当前为1,那就向0走,为0,向1走. #include<stdio.h> # ...

  2. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  3. 字典树-百度之星-Xor Sum

    Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...

  4. HDU 4825 Xor Sum 字典树+位运算

    点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  5. 2014百度之星第三题Xor Sum(字典树+异或运算)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  6. HDU--5269 ZYB loves Xor I (字典树)

    题目电波: HDU--5269 ZYB loves Xor I 首先我们先解决 ai xor aj 每个数转化为二进制  我们用字典树统计 每个节点 0 和 1 的出现的个数 #include< ...

  7. HDU--4825 Xor Sum (字典树)

    题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...

  8. 51nod 1295 XOR key 可持久化01字典树

    题意 给出一个长度为\(n\)的正整数数组\(a\),再给出\(q\)个询问,每次询问给出3个数,\(L,R,X(L<=R)\).求\(a[L]\)至\(a[R]\)这\(R-L+1\)个数中, ...

  9. CH 1602 - The XOR Largest Pair - [字典树变形]

    题目链接:传送门 描述在给定的 $N$ 个整数 $A_1, A_2,\cdots,A_N$ 中选出两个进行xor运算,得到的结果最大是多少? 输入格式第一行一个整数 $N$,第二行 $N$ 个整数 $ ...

随机推荐

  1. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  2. codeforces 709A A. Juicer(水题)

    题目链接: A. Juicer 题意: 给出n个橘子,汁漫出来了就倒出来,反正就是要求要倒几次; 思路: AC代码: #include <iostream> #include <cs ...

  3. 使用Unity开发Android的几种调试方法

    前言 本文举例几种Android 调试的方法(PS:我是通过unity引擎来开发安卓游戏) Eclipse + adt 查看LOG 1.为Eclipse 装上adt 插件 2.打开Eclipse 的L ...

  4. php strcmp引起的问题

    在官方的文档有这么一端说明: Note a difference between 5.2 and 5.3 versions echo (int)strcmp('pending',array()); w ...

  5. 使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇

    调用jQuery的ajax方法时,jQuery会根据post或者get协议对参数data进行序列化; 如果提交的数据使用复杂的json数据,例如: {userId:32323,userName:{fi ...

  6. 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 > ...

  7. Adivisor

    1.Adivisor是一种特殊的Aspect,Advisor代表spring中的Aspect 2.区别:advisor只持有一个Pointcut和一个advice,而aspect可以多个pointcu ...

  8. .net 4.0 自定义本地缓存策略的不同实现

    在分布式系统的开发中,为了提高系统运行性能,我们从服务器中获取的数据需要缓存在本地,以便下次使用,而不用从服务器中重复获取,有同学可能要问,为什么不使用 分布式缓存等,注意,服务器端肯定是考虑到扩展, ...

  9. HMAC-MD5算法原理及实现

    以下是分析节选,对于更详细的描述可以查阅RFC2104文档.     HMAC需要一个加密用散列函数(表示为H)和一个密钥K. 假设H是一个将数据块用一个基本的迭代压缩函数来加密的散列函数. 用B来表 ...

  10. SilverLight自定义ImageButton

    SilverLight中XAML的写法和WPF一样,但是发现在自定义按钮上,没有WPF来的容易,下面说说我制作SilverLight中的ImageButton的一些思路. 在SilverLight中, ...