SDNU 1141.Greatest Number【山东省第一届ACM】【7月22】
来源:互联网 发布:25端口是什么 编辑:程序博客网 时间:2024/06/10 07:16
Greatest Number
Description
Saya likes math, because she think math can make her cleverer.
One day, Kudo invited a very simple game:
Given N integers, then the players choose no more than four integers from them (can be repeated) and add them together. Finally, the one whose sum is the largest wins the game. It seems very simple, but there is one more condition: the sum shouldn’t larger than a number M.
Saya is very interest in this game. She says that since the number of integers is finite, we can enumerate all the selecting and find the largest sum. Saya calls the largest sum Greatest Number (GN). After reflecting for a while, Saya declares that she found the GN and shows her answer.
Kudo wants to know whether Saya’s answer is the best, so she comes to you for help. Can you help her to compute the GN?
One day, Kudo invited a very simple game:
Given N integers, then the players choose no more than four integers from them (can be repeated) and add them together. Finally, the one whose sum is the largest wins the game. It seems very simple, but there is one more condition: the sum shouldn’t larger than a number M.
Saya is very interest in this game. She says that since the number of integers is finite, we can enumerate all the selecting and find the largest sum. Saya calls the largest sum Greatest Number (GN). After reflecting for a while, Saya declares that she found the GN and shows her answer.
Kudo wants to know whether Saya’s answer is the best, so she comes to you for help. Can you help her to compute the GN?
Input
The input consists of several test cases.
The first line of input in each test case contains two integers N (0<n≤1000) and="" m(0<m≤="" 1000000000),="" which="" represent="" the="" number="" of="" integers="" upper="" bound.
Each of the next N lines contains the integers. (Not larger than 1000000000)
The last case is followed by a line containing two zeros.
The first line of input in each test case contains two integers N (0<n≤1000) and="" m(0<m≤="" 1000000000),="" which="" represent="" the="" number="" of="" integers="" upper="" bound.
Each of the next N lines contains the integers. (Not larger than 1000000000)
The last case is followed by a line containing two zeros.
Output
For each case, print the case number (1, 2 …) and the GN.
Your output format should imitate the sample output. Print a blank line after each test case.
Your output format should imitate the sample output. Print a blank line after each test case.
Sample Input
2 1010020 0
Sample Output
Case 1: 8
一开始想暴搜的,但是肯定超时!还是问了一下度娘。先枚举所有的两个数之和,然后4个数值和就变成了两个数之和,然后用二分法查找最优解,不然超时。贴代码:
#include<cstdio>#include<algorithm>using namespace std;int g[1000010],f[1000010];int main(){ int n,m,kase=0; while(scanf("%d%d",&n,&m),n||m){ f[0]=0; int sum=0,ka=0; for(int i=1;i<=n;i++) scanf("%d",&f[i]); sort(f,f+n+1); for(int i=0;i<=n;i++) for(int j=i;j<=n;j++) if(f[i]+f[j]<=m) g[ka++]=f[i]+f[j]; sort(g,g+ka); for(int i=0;i<ka;i++){ int low=i,high=ka-1; while(low<high){ int mid=(low+high)/2; if(g[i]+g[mid]<=m) low=mid+1; else high=mid-1; } if(g[i]+g[low]>sum&&g[i]+g[low]<=m) sum=g[i]+g[low]; } if(kase) printf("\n"); printf("Case %d: %d\n",++kase,sum); } return 0;}
0 0
- SDNU 1141.Greatest Number【山东省第一届ACM】【7月22】
- sdnu山东省ACM 2010年第一届省赛Greatest Number
- SDNU 1136.Balloons【山东省第一届ACM】【7月20】
- Greatest Number 山东省第一届ACM程序设计大赛
- 山东省第一届ACM大学生程序设计竞赛--Greatest Number--二分
- 山东省ACM第一届省赛 Greatest Number
- SDNU 1142.Hello World!【山东省第一届ACM】【7月21】
- SDNU 1143.Ivan comes again!【山东省第一届ACM】【7月21】
- [2010山东省第一届ACM大学生程序设计竞赛]——Greatest Number
- 2010年山东省第一届ACM大学生程序设计竞赛——Greatest Number
- 山东省第一届ACM省赛题——Greatest Number(不用dp的二分查找)
- SDUT 2157 Greatest Number (二分) 2010年山东省第一届ACM大学生程序设计竞赛
- 山东省第一届ACM省赛 G SDUT 2157 Greatest Number(暴力枚举)
- 山东省第一届ACM大学生程序设计竞赛 Greatest Number 二分查找
- 山东省第一届ACM大学生程序设计竞赛 problem D Greatest Number
- 山东省第一届ACM省赛题——Phone Number
- 山东省第一届ACM习题
- Phone Number 2010年山东省第一届ACM大学生程序设计竞赛
- 微信投票刷票的方法
- 利用WIFI(不用数据线)连接Android手机进行调试
- Snail—UI学习之UIImage及UIImageView
- Java对象生命周期
- NYOJ 35 表达式求值
- SDNU 1141.Greatest Number【山东省第一届ACM】【7月22】
- js 动画3 完美框架
- 循环结构小例子(1)
- HDU_2594 Simpsons’ Hidden Talents
- hdu 1878 欧拉回路
- ios的类别(category)和扩展(Extension)
- Android中的回调函数使用
- [HDU 1003] Max Sum
- Qt第一篇: