HDU 5536
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5536
题意:
1000个数里面,选三个下标不同的数构成函数(ai+aj)^ak。
求这个函数的最大值。
思路:
复现并没有做出来。
暴力竟然能过。
01Trie的话本质是贪心,把所有数按照二进制插入Trie里,枚举i和j,然后每次用32的常数级查询就能得到对应最大值。
1)即根据异或性质,如果两个数二进制表示上的相同位置数字相同则异或值为0,否则为1;
2)2^n > 2^1 + 2^2 + … + 2^(n-1)(这里表示幂次)。
源码:
暴力版:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
#define LL long long
const int MAXN = 1000 + 5;
LL d[MAXN];
vector<int>two[35];
LL max2(LL a)
{
LL ans = 1;
while(ans <= a) ans = ans << 1;
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
for(int i = 0 ; i < n ; i++)
scanf("%I64d", &d[i]);
LL ans = 0;
for(int i = 0 ; i < n ; i++){
for(int j = i + 1 ; j < n ; j++){
for(int k = j + 1 ; k < n ; k++){
ans = max(ans, (d[i] + d[j]) ^ d[k]);
ans = max(ans, (d[i] + d[k]) ^ d[j]);
ans = max(ans, (d[j] + d[k]) ^ d[i]);
}
}
}
printf("%I64d\n", ans);
}
return 0;
}
01Trie版:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int MAXN = 100000 * 2 + 5;
int cnt;
struct Trie
{
int son[2];
int num[2];
void init(){memset(son, 0, sizeof(son)); memset(num, 0, sizeof(num));}
}trie[MAXN * 2];
int d[1000 + 5];
void ins(int a)
{
int u = 0;
for(int i = 30 ; i >= 0 ; i--){
int mark;
if(a & (1 << i)) mark = 1;
else mark = 0;
if(trie[u].son[mark] == 0){
trie[u].son[mark] = ++cnt;
trie[u].num[mark] = 1;
}
else{
trie[u].num[mark]++;
}
u = trie[u].son[mark];
}
}
void del(int a)
{
int u = 0;
for(int i = 30 ; i >= 0 ; i--){
int mark;
if(a & (1 << i)) mark = 1;
else mark = 0;
trie[u].num[mark]--;
u = trie[u].son[mark];
}
}
int query(int a)
{
int ans = 0;
int u = 0;
for(int i = 30 ; i >= 0 ; i--){
int mark;
if(a & (1 << i)) mark = 1;
else mark = 0;
// if(mark == 0){
if(trie[u].num[mark^1] && trie[u].son[mark^1]){
u = trie[u].son[mark^1];
ans += 1 << i;
}
else{
u = trie[u].son[mark];
}
// }
// else{
// if(trie[u].num[mark^1] && trie[u].son[mark^1]){
// u = trie[u].son[mark^1];
// ans += 1 << i;
// }
// else{
// u = trie[u].son[mark];
// }
// }
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
for(int i = 0 ; i < n ; i++) scanf("%d", &d[i]);
// sort(d, d + n);
cnt = 0;
// for(int i = 0 ; i < MAXN * 2 ; i++) trie[i].init();
for(int i = 0 ; i < n ; i++) ins(d[i]);
int ans = 0;
for(int i = n - 1 ; i >= 0 ; i--){
del(d[i]);
for(int j = i - 1 ; j >= 0 ; j--){
del(d[j]);
ans = max(ans, query(d[i] + d[j]));
ins(d[j]);
}
ins(d[i]);
}
for(int i = 0 ; i <= cnt ; i++) trie[i].init();
printf("%d\n", ans);
}
return 0;
}