题意:从n个数中选出不同的三个数a b c,使得(a+b)^c 最大
分析:先将所有数字按位插入到字典树上,然后删除两个数字,贪心询问与剩下的数字最大异或值。
/************************************************ * Author :Running_Time * Created Time :2015/11/1 14:58:49 * File Name :J.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int N = 1e3 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; struct Trie { int ch[N*30][2], sz; int cnt[N*30]; void init(void) { sz = 1; ch[0][0] = ch[0][1] = 0; memset (cnt, 0, sizeof (cnt)); } void insert(int x) { int u = 0; for (int c, i=30; i>=0; --i) { c = x & (1 << i) ? 1 : 0; if (!ch[u][c]) { ch[sz][0] = ch[sz][1] = 0; ch[u][c] = sz++; } u = ch[u][c]; cnt[u]++; } } void remove(int x) { int u = 0; for (int c, i=30; i>=0; --i) { c = x & (1 << i) ? 1 : 0; u = ch[u][c]; cnt[u]--; } } int query(int x) { int u = 0; for (int c, i=30; i>=0; --i) { c = x & (1 << i) ? 1 : 0; if (c == 1) { if (ch[u][0] && cnt[ch[u][0]]) u = ch[u][0]; else u = ch[u][1], x ^= (1 << i); } else { if (ch[u][1] && cnt[ch[u][1]]) u = ch[u][1], x ^= (1 << i); else u = ch[u][0]; } } return x; } }trie; int a[N]; int main(void) { int T; scanf ("%d", &T); while (T--) { int n; scanf ("%d", &n); for (int i=1; i<=n; ++i) { scanf ("%d", &a[i]); } int ans = 0; trie.init (); for (int i=1; i<=n; ++i) { trie.insert (a[i]); } for (int i=1; i<=n; ++i) { trie.remove (a[i]); for (int j=i+1; j<=n; ++j) { trie.remove (a[j]); ans = max (ans, trie.query (a[i] + a[j])); trie.insert (a[j]); } trie.insert (a[i]); } printf ("%d ", ans); } return 0; }