uva 10125 - Sumsets

来源:互联网 发布:hao123网址软件 编辑:程序博客网 时间:2024/05/18 02:23

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

52 3 5 7 1252 16 64 256 10240

Output for Sample Input

12no 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;}

原创粉丝点击