zoukankan      html  css  js  c++  java
  • 用trie树解决最大异或对问题(On)

    在给定的N个整数A1A2ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少?

    输入格式

    第一行输入一个整数N。

    第二行输入N个整数A1A1~ANAN。

    输出格式

    输出一个整数表示答案。

    数据范围

    1N1051≤N≤105,
    0Ai<2310≤Ai<231

    输入样例:

    3
    1 2 3
    

    输出样例:

    3

    ##########################################################################
     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 const int N = 1e5+10;
     6 const int M = 300e4+10;
     7 int arr[N];
     8 int son[M][2];
     9 int flag[M], idx;
    10 
    11 void insert(int x){
    12     int k = 30;
    13     int p = 0;
    14     while(k >= 0){
    15         int tmp = x >> k & 1;
    16         if(!son[p][tmp]) son[p][tmp] = ++idx;
    17         p = son[p][tmp];
    18         --k;
    19     }
    20     flag[p]++;
    21 }
    22 int main(){
    23     int res = 0;
    24     int n;
    25     cin >> n;
    26     for(int i = 0;i < n;++i){
    27         cin >> arr[i];
    28         insert(arr[i]);
    29     }
    30     for(int i = 0;i < n;++i){
    31         int k = 30;
    32         int p = 0;
    33         int sum = 0;
    34         while(k >= 0){
    35             int tmp = arr[i]>>k & 1;
    36             if(son[p][!tmp]){
    37                  p = son[p][!tmp];
    38                  sum += 1 << k;
    39             }
    40             else p = son[p][tmp];
    41             --k;
    42         }
    43         res = max(res,sum);
    44     }
    45     cout << res ;
    46     return 0;
    47 }
    View Code

    end

  • 相关阅读:
    给任意多个链表然后要合并成一个
    hdu5967数学找规律+逆元
    poj2125最小点权覆盖+找一个割集
    poj3308 最小点权覆盖
    poj2987 最大闭合权子图基础题
    poj2699 转化为可行性判定问题+二分枚举+最大流
    判断割是否唯一zoj2587
    快排优化
    jvm垃圾收集器
    三次握手与四次挥手
  • 原文地址:https://www.cnblogs.com/sxq-study/p/12220880.html
Copyright © 2011-2022 走看看