博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ZOJ1101---Gamblers

Posted on 2010-07-18 12:36  还好  阅读(884)  评论(0编辑  收藏  举报

 

Gamblers

 

Time Limit: 1 Second      Memory Limit: 32768 KB

 

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

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

Output for Sample Input

12
no solution

 

转化一下题目,意思就是说在一组给定的数中,求出最大的一个数,这个数的和等于数组中其他三个数的和!

解题思路:

最容易想到的解法就是暴力搜索,不过这样待四重循环,而数组的元素的最大个数是1000,也就是说时间复杂度会达到O^4,这样显然会超时!所以肯定不行!一开始我也没想出来怎么做,感觉这个题目根本就不可能在1s内解决!后上网查了一下,解法就是先排序,在用二分搜索!

 

于是乎,小弟就按照这一思路去写代码,结果真的是一波三折,总的来说我代码很快写好了,排序方法可以用库函数,二分查找算法很容易也就写好了!本地测试貌似一没什么错误,但提交代码的时候就是一直Wrong Answer,真是郁闷啊!看着代码发呆了N久,后来才发现,原来是因为输入的每个数范围是 -536870912 到 +536870911,我刚开始的时候是先三个数相加,然后再在数组里面查找,但这里面数可以为负数,所以查找的时候,即是找到了,还需要保证这个数不是那相加的三个数当中的一个!

下面是我写的代码,仅供参考!运行时间:10MS,内存占用:184KB

 

源代码
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 bool BinarySearch(int*a, int begin,int end, int num)
 6 {
 7     if(begin>end)
 8     {
 9         return false;
10     }
11     int mid=(begin+end)/2;
12     if(num==a[mid])
13     {
14         return true;
15     }
16     else
17     {
18         if(num<a[mid])
19         {
20             return BinarySearch(a,begin,mid-1,num);
21         }
22         else
23         {
24             return BinarySearch(a,mid+1,end,num);
25         }
26     }
27     return false;
28 }
29 
30 int main(void)
31 {
32     int a[1001];
33     int n,i,j,k,place;
34     int num1,num2;
35     bool isExist;
36     cin>>n;
37     while(n)
38     {
39         isExist=false;
40         for(i=0;i<n;i++)
41         {
42             cin>>a[i];
43         }
44         sort(a,a+n);
45         for(i=n-1;i>=0;i--)
46         {
47             if(isExist)
48             {
49                 break;
50             }
51             for(j=0;j<n-1;j++)
52             {
53                 if(isExist)
54                 {
55                     break;
56                 }
57                 if(i==j)
58                 {
59                     continue;
60                 }
61                 num1=a[i]-a[j];
62                 for(k=j+1;k<n;k++)
63                 {
64                     if(k==i)
65                     {
66                         continue;
67                     }
68                     num2=num1-a[k];
69                     if(num2==a[i]||num2==a[j]||num2==a[k])
70                     {
71                         continue;
72                     }
73 
74                     if(BinarySearch(a,0,n-1,num2))
75                     {
76                         cout<<a[i]<<endl;
77                         isExist=true;
78                         break;
79                     }
80                 }
81             }
82         }
83         if(!isExist)
84         {
85             cout<<"no solution"<<endl;
86         }
87         cin>>n;
88     }
89     return 0;
90 }

 

另外循环的时候最好从最后一个数开始循环!