大意略。
思路:粗略估计了一下时间复杂度,应该过不去,然后想到了POJ上一道a^2+b^2 = c^2+d^2相类似的题,于是就去写了哈希,转换一下:a+b = d-c,数组A按照从小到大排列。然后从后往前枚举A[i]-A[j],这样保证A[i]-A[j]大于0而且A[i]是满足条件最大的,其中hash与find的时候,要保证编码,解码的类型操作元素是同一类型的。
至于整数哈希函数的设计的话,可以去看看这篇文章:http://wtommy.ycool.com/post.2717394.html
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int MAXN = 10000003;
const int MAXSIZE = 1030;
int n;
int rear;
struct node
{
int i, j;
}st[MAXSIZE*MAXSIZE];
int first[MAXN], next[MAXSIZE*MAXSIZE];
int A[MAXSIZE];
int sum[MAXSIZE*MAXSIZE];
void init()
{
rear = 0;
memset(first, -1, sizeof(first));
}
int hash(int s)
{
int h = s & 0x7fffffff;
return h % MAXN;
}
void insert(int s)
{
int h = hash(sum[s]);
next[s] = first[h];
first[h] = s;
}
int find(int i, int j, int s)
{
int h = hash(s);
for(int v = first[h]; v != -1; v = next[v])
{
if(s == sum[v] && st[v].i != i && st[v].i != j && st[v].j != i && st[v].j != j) return 1;
}
return 0;
}
void read_case()
{
init();
for(int i = 0; i < n; i++) scanf("%d", &A[i]);
sort(A, A+n);
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
st[rear].i = i, st[rear].j = j;
sum[rear] = A[i]+A[j];
insert(rear);
rear++;
}
}
}
void solve()
{
read_case();
for(int i = n-1; i >= 0; i--)
{
for(int j = 0; j < n; j++) if(i != j)
{
if(find(i, j, A[i]-A[j]))
{
printf("%d\n", A[i]);
return ;
}
}
}
printf("no solution\n");
}
int main()
{
while(scanf("%d", &n) && n)
{
solve();
}
return 0;
}