ZOJ-1101-Gamblers【你为什么还在超时,用二分吧】


Gamblers


Time Limit: 2 Seconds      Memory Limit: 65536 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



 

 【题意】

给n个数,是否存在一个最大的n使另外三个数相加== n;

 

改无数次。。超时点在代码中标出,以后写二分的时候我要注意了!! 参考博主ZOJ-1101-Gamblers

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <map>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define og(i,a,b) for(int i=a;i>=b;i--)
#define mem(a) memset(a,0,sizeof(a))
#define cs cout<<"-----"<<endl;
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn = 1e3 + 5;
const double pi = atan(1.)*4;
typedef long long ll;
int a[maxn];
int n;
int search(int pos,int x)//不加pos这个就超时!!!!!
{
    int l=pos,r=n,mid;
    while(l<=r)
    {
        mid = (l+r)/2;
        if(a[mid] < x)
            l = mid+1;
        else if(a[mid] > x)
            r = mid-1;
        else
            return mid;
    }
    return 0;
}
int work()
{
    for(int i=n;i>=1;i--)
    {
        for(int j = 1;j < i;j++)
        {
            for(int k = j+1;k<= n;k++)
            {
                int tmp = a[i] - a[j] - a[k];
                if(search(k,tmp)  && search(k,tmp) != i)
                    return i;//如果存在第三个赌徒的钱数符合要求,则记录下来赢家的编号
            }
        }
    }
   return -1;
}
int main()
{
    while(scanf("%d",&n)  && n)
    {
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a+1,a+n+1);
        int ans = work();
        if(ans == -1)
            printf("no solution\n");
        else
            printf("%d\n",a[ans]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值