最大異或對

2021-10-25 16:10:21 字數 802 閱讀 7147

在給定的n個整數a1,a2……an

中選出兩個進行xor(異或)運算,得到的結果最大是多少?

輸入格式

第一行輸入乙個整數n。

第二行輸入n個整數a1

~an。

輸出格式

輸出乙個整數表示答案。

資料範圍

1≤n≤105

,0≤ai<231

輸入樣例:

31 2 3

輸出樣例:

直接暴力會tle,考慮優化,用樹來儲存每乙個樹的二進位制,如下圖方式儲存

#include

using

namespace std;

const

int n=

100005

,m=31

*n;int a[n]

;int son[m][2

],idx;

void

insert

(int x)

//建立二進位制樹,每次加入x都是加入新枝條

}int

query

(int x)

//找到當前整數匹配得到的最大異或

else

}return res;

}int

main()

int res=0;

for(

int i=

0;i) cout<

}

最大異或對

acwing 143.最大異或對 在給定的n個整數a1,a2 an中選出兩個進行xor 異或 運算,得到的結果最大是多少?輸入格式第一行輸入乙個整數n。第二行輸入n個整數a1 an。輸出格式輸出乙個整數表示答案。資料範圍1 n 105,0 ai 231 輸入樣例 3 1 2 3 輸出樣例 題解 異或...

最大異或對

最大異或對 題解 這題採用的是01字典樹,一般這種異或問題都採用的是01字典樹樹解決。includeusing namespace std const int n 2e6 1 typedef long long ll ll a n int cnt 1 int tree 3 n 2 void inse...

最大異或對

1 對a i 異或 0 1 建立trie陣列。2 從trie數從根開始遍歷,找與a i 相反的數 3 找到當前不一樣的分支,並往前走。1 建trie 2 與輸入二進位制,選擇反方向異或,得到最大值 3 最後返回最大值7。在給定的n個整數a1,a2 ana1,a2 an中選出兩個進行xor 異或 運算...