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. JAVscript对象
  2. 推荐系统(1)--splitting approaches for context-aware recommendation
  3. UVA10763交换学生
  4. The Road to SDN: An Intellectual History of Programmable Networks
  5. 【C语言】三子棋游戏
  6. Instruments性能优化-Core Animation
  7. Huawei is developing a new future technology
  8. 力扣-图解算法数据结构
  9. Python unittest –单元测试示例
  10. Spring Cloud学习笔记---Spring Cloud Sleuth--一个手动搭建zipkin碰到的坑
  11. mybatis比mysql安全吗_MyBatis 和 SQL 注入的恩恩怨怨
  12. 百度指数查关键词(惊到我啦)
  13. 面向Web应用的并发压力测试工具——Locust实用攻略
  14. c语言怎么编程机器人,移动机器人(电子球)编程(c语言)
  15. photoshopCS6软件的安装和破解方法
  16. MT6577/MT6589处理器参数对比分析
  17. MTCNN论文翻译 人脸检测
  18. Python爬虫 爬取新浪微博热搜
  19. Bugku 分析 特殊后门(wireshark流量包分析)
  20. 20170627总结

热门文章

  1. 修改Element-ui中tree组件最底层节点的样式
  2. 长理2017 f0803 求和:s=1-(1/2)+(1/3)-(1/4).........+(1/n),编写函数fun(char*s),实现字符串的逆置编写程序从基类派生圆柱,设计(均为可运行代码)
  3. 【云原生】Docker网络原理及Cgroup硬件资源占用控制
  4. matlab henon,Henon系统动力学行为的MATLAB仿真研究
  5. matlab的sub,matlab中的通用置换指令subs()用法
  6. GOES-16数据下载(保姆级教程)
  7. 学C++还是学Java?做软件研发还需掌握哪些知识和技能?
  8. 使用dreamweavernbsp;制作网页幻灯片
  9. 手机app考勤软件有哪些优点?
  10. Haproxy + pacemaker + fence