uva 10125 - Sumsets

Problem C - Sumsets

Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S.

Input

Several S, each consisting of a line containing an integer 1 <= n <= 1000 indicating the number of elements in S, followed by the elements of S, one per line. Each element of S is a distinct integer between -536870912 and +536870911 inclusive. The last line of input contains 0.

Output

For each S, a single line containing d, or a single line containing "no solution".

Sample Input

5
2 
3 
5 
7 
12
5
2 
16 
64 
256 
1024
0

Output for Sample Input

12
no solution
a,b,c,d互异,
开始直接排完序三重循环+二分wrong到死,忽略了个问题不该每次穷举a+b+c,这样无法保证每次都是最大的,穷举d,a,b,再二分查c终于水过了
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int a[1010],i,j,k,ans,f,n;
int comp(int x,int y)
{return x>y;};
int find(int key,int l,int r)
{int mid=(r+l)/2;
 if (l>r) return 0;
 if (key==a[mid])
 {if ((mid!=i)&&(mid!=j)&&(mid!=k)) f=1;
  return 0;
 }
 else
 if (key<a[mid])  find(key,mid+1,r);
            else  find(key,l,mid-1);
};
int main()
{while (scanf("%d",&n),n)
 {
  for (i=0;i<n;i++)
  scanf("%d",&a[i]);
  sort(a,a+n,comp);
  f=0;
  for (i=0;i<n;i++)
  for (j=0;j<n-1;j++)
  for (k=j+1;k<n;k++)
  {ans=a[i];
   if ((i!=j)&&(i!=k))
   find(a[i]-a[j]-a[k],0,n-1);
   if (f) goto there;
  }
  there:;
  if (f) printf("%d\n",ans);
     else printf("no solution\n");
 }
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值