https://vjudge.net/contest/168758#problem/J
给你一个数组。
要求选三个数 (a+b)^c 最大(不能选下标相同的数。)
8600ms飘过
两两枚举。然后在枚举c。
这样貌似比直白的三重循环快一点
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn=1050;
ll a[maxn];
struct Node
{ int first;
int second;
ll all;
Node(int _a,int _b,ll _c){first=_a;second=_b;all=_c;}
};
vector<Node>sum;
int main()
{ int m;
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&m);
sum.clear();
for(int i=1;i<=m;i++)
scanf("%lld",&a[i]);
for(int i=1;i<=m;i++){
for(int j=i+1;j<=m;j++){
sum.push_back(Node(i,j,a[i]+a[j]));
}
}
ll ans=-1;
for(int i=0;i<sum.size();i++){
for(int j=1;j<=m;j++){
if(sum[i].first!=j&&sum[i].second!=j){
ans=max(ans,a[j]^sum[i].all);
}
}
}
printf("%lld\n",ans);
}
return 0;
}
字典树的方法有时间再补。等我把数据结构实习忙完了,因为字典树我不会,还要学习,而我又是不愿学习的qwq
9-1,等了差不多两个月qwq、
01字典树是字典树的一种特殊情况。用来计算异或最大这种情况。
就是把一个数当成 二进制然后从高位到低位存到 一个只有 0和1的字典树里,然后每次尽量找和 目标数当前位 不相同的方向(因为是异或)
#include <bits/stdc++.h>
using namespace std;
/* 这道题也可以用01字典树。
这个是带删除的方法,
用数组模拟。
通过给
ch里存的是下一个节点的下标。
而下标并不是就开好的,而是慢慢加的,
val是当前点的经过。
*/
const int maxn=32000+1000;
int ch[maxn][2];
int val[maxn];
int a[maxn];
int sz;//表示标号。
void init(){
sz=1;//初始那个标号为1,
memset(ch,0,sizeof(ch));
}
void update(int v,int d){
int u=0;
for(int i=31;i>=0;i--){
int sign=(v>>i)&1;
if(!ch[u][sign]){
//memset(ch[sz],0,sizeof(sz));
ch[u][sign]=sz;
val[sz]=0;
sz++;
//val[sz]=0;
}
u=ch[u][sign];
val[u]+=d;
}
}
int query(int v) {
int ans = 0, u = 0;
for(int i = 31; i >= 0; i--) {
int c = (v >> i) & 1;
if(ch[u][c^1] && val[ch[u][c^1]]) {
ans |= (1 << i);
u = ch[u][c^1];
}
else u = ch[u][c];
}
return ans;
}
int main()
{ int t,m;
scanf("%d",&t);
while(t--){
memset(ch,0,sizeof(ch));
//memset(val,0,sizeof(val));
init();
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&a[i]);
update(a[i],1);
}
int ans=-1;
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
if(i==j) continue;
update(a[i],-1);//删掉
update(a[j],-1);//删掉一个
ans=max(ans,query(a[i]+a[j]));
//cout<<ans<<endl;
update(a[i],1);//再加上
update(a[j],1);//再加上、
}
}
printf("%d\n",ans);
}
return 0;
}