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;}
- Gamblers
- Gamblers Problem
- 1101 Gamblers
- zoj1101 Gamblers
- ZJU 1101 Gamblers
- zoj 1101 Gamblers
- 五天一小水 D - Gamblers
- zoj 1101 Gamblers
- zoj 1101 Gamblers
- investors and gamblers
- ZOJ 1101Gamblers
- ZOJ 1101 Gamblers
- ZOJ 1101 Gamblers
- ZOJ 1101 Gamblers 二分
- ZOJ1101-Gamblers 终于AC。。。
- zoj 1101 Gamblers
- ZOJ 1101 Gamblers
- zoj Gamblers(hash)
- Windows如何打包Qt程序
- insert数据时,报ORA-01555 22924错误
- Android开发点点滴滴——一些基础的但有用的知识
- Qt Creator中常用快捷键
- java.lang.OutOfMemoryError: PermGen space及其解决方法
- Gamblers
- webrtc 学习笔记1——总览
- 仿迅雷播放器教程
- RMAN 简单介绍
- [Qt教程] 第17篇 2D绘图(七)涂鸦板
- 数据库中查询前n项以及排序
- PHP PDO(mysql) 封装类
- Oracle执行SQL语句的过程
- iOS 5设备不能通过plist配置文件安装ipa程序