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?

输入

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
解题:先循环一下,两个两个的相加一下,然后二分查找,时间算好,不会超过1s;
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 1e6+;
#define LL long long
LL sum[N];
LL a[N];
int search(int l,int r,LL num)
{
int mid;
while(l<r)
{
mid = (l+r)/;
if(sum[mid]<=num) l = mid+;
else r = mid;
}
return l-;
}
int main()
{
int m,k,cnt = ;
LL x,n;
//freopen("greatest.in","r",stdin);
while(scanf("%d %lld",&m,&n) && m+n)
{
k = ;
for(int i=;i<=m;i++)
{
scanf("%lld",&x);
if(x<=n)
a[k++] = x;
}
a[k]=;
int kk = ;
for(int i=;i<=k;i++)
for(int j=;j<=k;j++)
{
if(a[i] + a[j] <=n)
sum[kk++] = a[i]+a[j];
}
sort(sum,sum+kk);
LL ans = ;
for(int i=;i<kk;i++)
{
LL M = n - sum[i];
int x = search(,kk,M);
ans = max(ans,sum[i]+sum[x]);
}
printf("Case %d: %lld\n\n",cnt++,ans);
}
return ;
}

最新文章

  1. PHP获取客户端IP
  2. DataGridView
  3. Html5应用程序缓存ApplicationCache
  4. go语言条件语句 if else
  5. centos6.5 iptables结合ipset批量屏蔽ip
  6. VS2010工程文件减肥
  7. ASP中可能出现的一种包含漏洞(Server.execute)
  8. 针对各种浏览器css不兼容的写法
  9. Debian 8.0(Jessie) 无线网卡,ATI显卡驱动和输入法等安装记录。
  10. DLL入门浅析(3)——从DLL中导出变量
  11. (二十六)静态单元格(Cell)
  12. iOS 开发之 KVC - setValuesForKeysWithDictionary 解析
  13. Oracle 使用PDB 的情况下进行备份恢复的使用.
  14. 015.Zabbix的日志监控配置
  15. Angular CLI 使用教程指南参考
  16. (转)Java程序员应该知道的10个调试技巧
  17. TableView,自定义TableViewCell
  18. js如何创建JSON对象
  19. Android 开发工具类 24_getHtml
  20. 查找Python包的依赖包(语句)

热门文章

  1. openwrt开源系统LUCI配置界面
  2. sonar如何添加自定义JAVA规则
  3. Javascript函数式编程的一些例子[转载]
  4. urllib库在python2和python3环境下的使用区别
  5. (转)NIO 文件锁定
  6. java实现快速排序算法
  7. 无废话MVC入门教程一[概述、环境安装、创建项目]
  8. hdu1700 Points on Cycle (数学)
  9. 模块化开发RequireJS之shim配置
  10. [Mac A]为什么国外程序员爱用 Mac?