[2010山东省第一届ACM大学生程序设计竞赛]——Greatest Number

本文介绍了2010山东省第一届ACM大学生程序设计竞赛中的一道题目——Greatest Number。问题要求在不超过4个数字的情况下,组成一个最大且不大于特定值M的数。通过举例说明,例如N=2, M=10,最优解是使用数字2四次得到8。解题策略是将可能的0个、1个、2个数字的情况放入数组,并进行排序,然后使用二分查找找到符合条件的组合。" 5034013,660454,InnoDB表锁与行级锁详解,"['数据库', 'mysql', '事务', '行锁', '表锁']
摘要由CSDN通过智能技术生成
Greatest Number

题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2157

题目描述

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?


输入

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 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.


示例输入

2 10
100
2

0 0


示例输出

Case 1: 8


题目要求就是  给你N个数,用不多于4个数组成不大于M的最大的数,所给的数使用次数不限。

用样例解释:N为2,M为10,就是用2和100组成一个不大于10的最大的数。

100大于10了,显然舍弃,剩下2可以用多次,因为最多用4个数组成一个数,所以2用4次得到8。


这道题的解题思路比较巧妙,要求组成一个数的个数小于等于4,我们就可以让组成0个数的(也就是0),一个数的(输入的数本身),和所有任意两个数的和存入一个数组中。

这样数组内任意两个数相加的和 就代表了任意的1~4个数组成的数。

最后再用二分查找,找一下符合要求的,就OK了。~\(≧▽≦)/~啦啦啦

对了,在查找前不要忘了排序呀。


#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int arr[1001000];
int N,M;

// 二分查找
int binary_sort(int len)
{
    int i,l,h,mid,aim;
    aim=0;
    for(i=0; i<len; ++i)
    {
        l=0;h=len;
        while(l<h)                    //对当前的数用二分查找法查找另一个满足要求的数
        {
            mid=(l+h)/2;
            if(arr[mid]+arr[i]<=M)
            {
                if(arr[i]+arr[mid]>=aim)
                    aim=arr[i]+arr[mid];
                l=mid+1;
            }
            else
                h=mid;
        }
    }
    return aim;
}
int main()
{
    int i,j,k,len,test,temp;
    test=1;

    while(cin>>N>>M)
    {
        if(!N && !M)    break;
        memset(arr,0,sizeof(arr));
        k=1;
        // 将每个数都存入数组
        for(i=0;i<N;++i)
        {
            cin>>temp;
            if(temp>M)  continue;
            arr[k++]=temp;
        }
        // 如果输入数据中有最大值,直接输出.

        len=k;
        // 将任意两个数的和存入数组
        for(i=1;i<len;++i)
            for(j=1;j<len;++j)
                arr[k++]=arr[i]+arr[j];
        // 由小到大排序,便于后面二分查找
        sort(arr,arr+k);

        cout<<"Case "<<test++<<": "<<binary_sort(k)<<endl;
        cout<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值