Greatest Number 山东省第一届省赛
Greatest Number
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
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 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.
输出
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 ;
}
Greatest Number 山东省第一届省赛的更多相关文章
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- Sdut 2151 Phone Numbers (山东省ACM第一届省赛题 A)
题目描述 We know thatif a phone number A is another phone number B's prefix, B is not able to becalled. ...
- ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题)
题目描述 We know thatIvan gives Saya three problems to solve (Problem F), and this is the firstproblem. ...
- 【容斥】Four-tuples @山东省第九届省赛 F
时间限制: 10 Sec 内存限制: 128 MB 题目描述 Given l1,r1,l2,r2,l3,r3,l4,r4, please count the number of four-tuples ...
- 【二分图带权匹配】Anagram @山东省第九届省赛 A
题目描述 Orz has two strings of the same length: A and B. Now she wants to transform A into an anagram o ...
- 【二分图最大匹配】Bullet @山东省第九届省赛 B
时间限制: 6 Sec 内存限制: 128 MB 题目描述 In GGO, a world dominated by gun and steel, players are fighting for t ...
- 第一届山东省ACM——Phone Number(java)
Description We know that if a phone number A is another phone number B’s prefix, B is not able to be ...
- 第一届山东省ACM——Balloons(java)
Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...
- 河南省第十届省赛 Plumbing the depth of lake (模拟)
title: Plumbing the depth of lake 河南省第十届省赛 题目描述: There is a mysterious lake in the north of Tibet. A ...
随机推荐
- Oracle错误代码ORA-01653,表空间容量无法扩展
业务模块在进行增操作时,报错“Caused by: java.sql.BatchUpdateException: ORA-01653: 表 JAZZ_V3.T_MZ_BK 无法通过 128 (在表空间 ...
- maven本地仓库地址的设置
对于大公司的jenkins来说,仓库是很大的,那么存储仓库的目录空间一定要足够大才可以. 可以对linux进行外挂,实现磁盘扩容,把仓库挂在外挂上. 默认情况下,mvn的配置文件在~/.m2/sett ...
- 关于TagHelper的那些事情——TagHelper的基本知识
概要 TagHelper是ASP.NET 5的一个新特性.也许在你还没有听说过它的时候, 它已经在技术人员之间引起了大量讨论,甚至有一部分称它为服务器控件的回归.实际上它只不过是一个简化版本,把HTM ...
- time_t和SYSTEMTIME之间的相互转换 【转】
time_t和SYSTEMTIME之间的相互转换 #include <ctime> /* **time_t转SYSTEMTIME */ SYSTEMTIME TimetToSystemTi ...
- python 将编码转为汉字
print '\u57fa\u7840\u5316\u5de5\u4e1a'.decode('unicode-escape') print urllib.unquote("%C0%FA%CA ...
- Solidworks的Toolbox拖出来的零件另存也没用,重新打开之后被自动替换怎么办
工具-选项-系统选项-异型孔向导,取消勾选"将此文件夹设为Toolbox零部件的默认搜索位置" 这样把Toolbox的零部件另存之后,就可以修改,比如添加草图和特征,然后另存之 ...
- Moving Tables-贪心
id=19566" target="_blank" style="color:blue; text-decoration:none">Movin ...
- 从零開始开发Android版2048 (三)逻辑推断
近期工作比較忙,所以更新的慢了一点,今天的主要内容是关于Android版2048的逻辑推断,经过本篇的解说,基本上完毕了这个游戏的主体部分. 首先还是看一下,我在实现2048时用到的一些存储的数据结构 ...
- 算法笔记_114:等额本金(Java)
1 等额本金 标题:等额本金 小明从银行贷款3万元.约定分24个月,以等额本金方式还款. 这种还款方式就是把贷款额度等分到24个月.每个月除了要还固定的本金外,还要还贷款余额在一个月 中产生的利息. ...
- 算法笔记_069:Floyd算法简单介绍(Java)
目录 1 问题描述 2 解决方案 2.1 使用Floyd算法得到最短距离示例 2.2 具体编码 1 问题描述 何为Floyd算法? Floyd算法功能:给定一个加权连通图,求取从每一个顶点到其它所 ...