本文主要是介绍sdut2157 Greatest Number,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Greatest Number
Time Limit: 1000ms Memory limit: 65536K
题目描述
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?
输入
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 and the 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 and the 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.
输出
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.
示例输入
2 10 100 20 0
示例输出
Case 1: 8
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1001005];
int n,m;
int main(){int i,j,cnt,aa;int icase=0;while(scanf("%d%d",&n,&m)&&n!=0&&m!=0){a[0]=0;cnt=1;for(i=1;i<=n;i++){scanf("%d",&aa);if(aa<=m)a[cnt++]=aa;}int cnt1=cnt;for(i=1;i<cnt1;i++)for(j=i;j<cnt1;j++)if(a[i]+a[j]<=m)a[cnt++]=a[i]+a[j];sort(a,a+cnt);int l,r=cnt-1,cur=cnt-1,mid,tmp,maxx=0;for(i=1;i<=cnt;i++){l=i;while(l<=r){mid=(l+r)/2;tmp=a[i]+a[mid];if(tmp>m)r=mid-1;else if(tmp<m){l=mid+1;if(maxx<tmp){cur=mid;maxx=tmp;}}else{maxx=tmp;i=cnt;break;}}r=cur;}printf("Case %d: %d\n",++icase,maxx);printf("\n");}return 0;
} /**************************************Problem id : SDUT OJ 2157 Result : Accepted Take Memory : 2444K Take Time : 140MS Submit Time : 2013-05-20 23:40:00
**************************************/
这篇关于sdut2157 Greatest Number的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!