题意:

给定一个整数集合,找出最大的d,使得a+b+c=d,a,b,c,d是集合中不同的元素;

思路:

如果单纯的枚举a,b,c的复杂度是O(n^3)的,为了降低复杂度,可以先把a+b的情形都找出来,然后再枚举d和c,是否符合要求;

AC代码:

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=3e6+10;
const int maxn=1e3+10;
const double eps=1e-10;

int a[maxn],n,ans;

struct node
{
    int a,b;
};
vector<node>ve[1000*maxn];
map<int,int>mp;


int check(int x,int y)
{
        int temp=mp[x-y];
        int len=ve[temp].size();
        For(k,0,len-1)
        {
             int fa=ve[temp][k].a,fb=ve[temp][k].b;
             if(fa!=x&&fa!=y&&fb!=x&&fb!=y)
             {
                 ans=max(ans,x);
                 return x;
             }
        }
        return -inf;
}
int main()
{
        while(1)
        {
            mp.clear();
            For(i,0,1000*maxn-1)ve[i].clear();
            read(n);
            if(!n)break;
            int cnt=0;
            For(i,1,n)read(a[i]);
            For(i,1,n)
                For(j,i+1,n)
                  {
                    if(!mp[a[i]+a[j]])
                    {
                        cnt++;
                        mp[a[i]+a[j]]=cnt;
                        node d;
                        d.a=a[i],d.b=a[j];
                        ve[cnt].push_back(d);
                    }
                    else 
                    {
                        int temp=mp[a[i]+a[j]];
                        node d;
                        d.a=a[i],d.b=a[j];
                        ve[temp].push_back(d);
                    }
                  }
            ans=-inf;
            For(i,1,n)
            {
                For(j,i+1,n)
                {
                    if(mp[a[i]-a[j]])ans=max(ans,check(a[i],a[j]));
                    if(mp[a[j]-a[i]])ans=max(ans,check(a[j],a[i]));
                }
            }
            if(ans==-inf)cout<<"no solution"<<endl;
            else cout<<ans<<endl;
        }
        return 0;
}