题目:
http://acm.hdu.edu.cn/showproblem.php?pid=5536
题意:
给出
n
个数,从这些数字中任选
思路:
暴力居然可过。。。然后正解是用字典树去求最大异或值,首先把
n
个数字插入到
#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 10, M = 2, INF = 0x3f3f3f3f;
struct node
{
node *next[M];
int val;
void init()
{
memset(next, 0, sizeof next);
val = 0;
}
}trie[N*32], *root;
int tot;
int len = 31;
node *new_node()
{
trie[tot].init();
return trie + tot++;
}
void trie_init()
{
tot = 0;
root = new_node();
}
void trie_insert(int x)
{
node *p = root;
for(int i = len-1; i >= 0; i--)
{
int j = 1 & (x>>i);
if(p->next[j] == NULL) p->next[j] = new_node();
p->next[j]->val++;
p = p->next[j];
}
}
int trie_query(int x)
{
node *p = root;
int ans = 0;
for(int i = len-1; i >= 0; i--)
{
int j = ! (1 & (x>>i));
if(p->next[j] != NULL && p->next[j]->val != 0)
{
p = p->next[j];
ans = ans * 2 + 1;
}
else
{
p = p->next[!j];
ans = ans * 2 + 0;
}
}
return ans;
}
void trie_del(int x)
{
node *p = root;
for(int i = len-1; i >= 0; i--)
{
int j = 1 & (x>>i);
p->next[j]->val--;
p = p->next[j];
}
}
int main()
{
int t, n;
int a[N];
scanf("%d", &t);
while(t--)
{
trie_init();
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
trie_insert(a[i]);
}
int ans = 0;
for(int i = 1; i <= n; i++)
for(int j = i+1; j <= n; j++)
{
trie_del(a[i]); trie_del(a[j]);
ans = max(ans, trie_query(a[i] + a[j]));
trie_insert(a[i]); trie_insert(a[j]);
}
printf("%d\n", ans);
}
return 0;
}
暴力代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 10, INF = 0x3f3f3f3f;
int a[N];
int main()
{
int t, n;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
int ans = 0;
for(int i = 1; i <= n; i++)
for(int j = i+1; j <= n; j++)
for(int k = j+1; k <= n; k++)
{
ans = max(ans, (a[i] + a[j]) ^ a[k]);
ans = max(ans, (a[j] + a[k]) ^ a[i]);
ans = max(ans, (a[i] + a[k]) ^ a[j]);
}
printf("%d\n", ans);
}
return 0;
}