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;}elseh=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;
}

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

  1. 山东省第五届ACM大学生程序设计竞赛 Weighted Median

    Weighted Median Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 For n elements x1, x2, .. ...

  2. 山东省第五届ACM大学生程序设计竞赛 Colorful Cupcakes

    Colorful Cupcakes Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Beaver Bindu has N cupc ...

  3. 2013年山东省第四届ACM大学生程序设计竞赛 Problem I Thrall’s Dream 图,2n遍dfs

    Thrall's Dream Time Limit: 1000MS Memory limit: 65536K 题目描述 We never paid any heed to the ancient pr ...

  4. 2013年山东省第四届ACM大学生程序设计竞赛

    Rescue The Princess Time Limit: 1000MS    Memory limit: 65536K 题目描述 Several days ago, a beast caught ...

  5. 河南计算机程序大赛,我院成功举办河南省第十一届ACM大学生程序设计竞赛

    5月26-27日,由ACM/ICPC亚洲区竞赛组委会授权.河南省计算机学会主办.我校承办的河南省第十一届ACM大学生程序设计竞赛在我校举行.来自省内30余所本科院校的215支参赛队645名学生参加了竞 ...

  6. 2019河南省第十二届ACM大学生程序设计竞赛参赛感

    盼望着,盼望着,acm省赛终于来了.2019年5月6日,一大早我们三个队就从郑州出发前往驻马店市--黄淮学院,一首<写给黄淮>火起来的大学.第一次听说黄淮学院还是高三时候韩老师天天对我们的 ...

  7. 郑州大学计算机系王院长,我院成功承办河南省第十二届ACM大学生程序设计竞赛...

    5月6日至7日,由河南省计算机学会主办.我校承办的河南省第十二届ACM大学生程序设计竞赛在体育中心隆重举行.来自全省45所高校的231支参赛队近700名学生及200名带队教练参加了竞赛,参赛院校.参赛 ...

  8. 河南省第五届acm大学生程序设计竞赛总结

    河南省第五届acm大学生程序设计竞赛总结 河南省第五届acm大学生程序设计竞赛最终排名 首先热烈祝贺我校ACM队在本次比赛中获得四金三银的成绩,而我们队也获得金牌一枚!!! 五月十三号河南省第五届ac ...

  9. 河南工程学院第五届ACM大学生程序设计竞赛(部分题解)

    河南工程学院第五届ACM大学生程序设计竞赛(部分题解) 问题 A: 敏感的小明同学 小明是一个对数字非常敏感的人,当他看到某个特定的数字p (1<=p<=9)时就会兴奋一下,现在给你一个数 ...

最新文章

  1. 用prototype 方式来创建客户端组件类
  2. MFC六大核心机制之二:运行时类型识别(RTTI)
  3. 一个有关Update类型的存储过程的问题
  4. 多线程循环输出abcc++_C ++循环| 查找输出程序| 套装2
  5. 汇编语言(王爽 第三版)检测点
  6. 【翻译】在Ext JS应用程序中构建可维护的控制器
  7. MATLAB点击运行并计时没反应,MATLAB计时器对象陷阱和不良用法
  8. 信息系统开发平台OpenExpressApp -如何部署OEA应用
  9. 关于style设置方法,obj.setAttribute(style.)和obj.style.stylename
  10. 自动修改mysql5.7初始化密码
  11. android apk 在线分析,Android Apk安装过程解析
  12. Origin双Y轴柱状图画法及两柱重合有间居问题解决
  13. 前端vs图片:2 图片深度、图片分类等基本信息
  14. 徐姗姗 20190905-3 命令行和控制台编程
  15. Python基础(8)字符串及常用操作
  16. PLSQL连接Oracle 数据库配置详解
  17. python多元线性回归报错(assert pytype not in (tokenize.NL, tokenize.NEWLINE))
  18. 【调研】成功的IT公司的老板几乎都是技术出身
  19. SEER区块链database_api更新 支持通过txid查询交易所在区块信息
  20. fMRI之dpabi处理经验(一)

热门文章

  1. python 使用turtle 画樱花(python3验证ok)
  2. Qt的QString类型
  3. 使用最新版本Android NDK-r21 编译 opencv-3.3.1 + opencv_contrib-3.3.1
  4. 36氪WISE 2018新商业大会召开 与时代前沿者共探新经济发展
  5. 【python小技巧】花式表白之动态二维码,这个gif有点帅哦
  6. 《简单的逻辑学》读后感
  7. 房贷计算器。在左侧输入数据,点击“开始计算”后,右侧右侧计算出结果后直接显示出来。
  8. 红蓝对抗之隧道技术第二篇(reGeorg内网穿透、SSH隧道本地Socks代理、SSH远程转发、Earthworm Socks5代理、Tunna正向代理、ICMP隧道、DNS隧道、Frp穿透)
  9. 每天学习写论文Day44 怎样找文献、下载文献?
  10. uC/OS-III源码下载(版本2009-2021)