Trie UVALive 7192 Chip Factory (15长春J)
生活随笔
收集整理的這篇文章主要介紹了
Trie UVALive 7192 Chip Factory (15长春J)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
題目傳送門(mén)
題意:從n個(gè)數(shù)中選出不同的三個(gè)數(shù)a b c,使得(a+b)^c 最大
分析:先將所有數(shù)字按位插入到字典樹(shù)上,然后刪除兩個(gè)數(shù)字,貪心詢問(wèn)與剩下的數(shù)字最大異或值。?
/************************************************ * 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\n", ans);}return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/Running-Time/p/4956062.html
總結(jié)
以上是生活随笔為你收集整理的Trie UVALive 7192 Chip Factory (15长春J)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。