Gamblers

来源:互联网 发布:网络新媒体技术杂志 编辑:程序博客网 时间:2024/05/22 07:54

A group of n gamblers decide to play a game:

At the beginning of the game each of them will cover up his wager on the table and the assitant must make sure that there are no two gamblers have put the same amount. If one has no money left, one may borrow some chips and his wager amount is considered to be negative. Assume that they all bet integer amount of money.

Then when they unveil their wagers, the winner is the one who's bet is exactly the same as the sum of that of 3 other gamblers. If there are more than one winners, the one with the largest bet wins.

For example, suppose Tom, Bill, John, Roger and Bush bet $2, $3, $5, $7 and $12, respectively. Then the winner is Bush with $12 since $2 + $3 + $7 = $12 and it's the largest bet.

Input

Wagers of several groups of gamblers, each consisting of a line containing an integer 1 <= n <= 1000 indicating the number of gamblers in a group, followed by their amount of wagers, one per line. Each wager is a distinct integer between -536870912 and +536870911 inclusive. The last line of input contains 0.

Output

For each group, a single line containing the wager amount of the winner, 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  如果存在d,求d的最大值,没有就输出no solution.
    
    解题报告:首先进行排序,进行预处理,把两个数之和存储起来,从输入排序后的数组后面开始进行遍历
              寻找两个数,从预处理中是否能找出这两个数的差,如果能,则存在,否则,就不存在。  
*/
//标程:
#include<stdio.h>#include<algorithm>using namespace std;#define  maxn 1005int a[maxn];struct ss{int x,y,z;}p[maxn*maxn];bool cmp(ss c, ss d){if(c.z!=d.z) return c.z<d.z;else if(c.x!=d.x) return c.x<d.x;else if(c.y!=d.y) return c.y<d.y;}int fun(int r,int sum){     int l=0,mid; while(l<r) { mid=(l+r)/2; if(p[mid].z<sum) l=mid+1; else r=mid; } return r;}int main(){//freopen("a.txt","r",stdin);int len,n,i,j,k,temp;    while(scanf("%d",&n),n){if(n<4) { printf("no solution"); continue;}for(i=0;i<n;i++) scanf("%d",a+i);        sort(a,a+n);for(i=0,len=0;i<n;i++)        for(j=i+1;j<n;j++)        {            p[len].x=a[i];            p[len].y=a[j];            p[len].z=a[i]+a[j];            len++;        }        sort(p,p+len,cmp);k=maxn;for(i=n-1;i>=0;i--)for(j=n-1;j>=0;j--)if(i!=j){temp=fun(len,a[i]-a[j]);while(p[temp].z==a[i]-a[j]){if(p[temp].x!=a[i] && p[temp].x!=a[j] && p[temp].y!=a[i] && p[temp].y!=a[j]){k=i; break;}temp++;}if(k==i)  j=i=-5;}if(i==-6) printf("%d\n",a[k]);else printf("no solution\n");}return 0;}
原创粉丝点击