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

Greatest Number 山东省第一届省赛的更多相关文章

  1. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

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

  3. ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题)

    题目描述 We know thatIvan gives Saya three problems to solve (Problem F), and this is the firstproblem. ...

  4. 【容斥】Four-tuples @山东省第九届省赛 F

    时间限制: 10 Sec 内存限制: 128 MB 题目描述 Given l1,r1,l2,r2,l3,r3,l4,r4, please count the number of four-tuples ...

  5. 【二分图带权匹配】Anagram @山东省第九届省赛 A

    题目描述 Orz has two strings of the same length: A and B. Now she wants to transform A into an anagram o ...

  6. 【二分图最大匹配】Bullet @山东省第九届省赛 B

    时间限制: 6 Sec 内存限制: 128 MB 题目描述 In GGO, a world dominated by gun and steel, players are fighting for t ...

  7. 第一届山东省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 ...

  8. 第一届山东省ACM——Balloons(java)

    Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...

  9. 河南省第十届省赛 Plumbing the depth of lake (模拟)

    title: Plumbing the depth of lake 河南省第十届省赛 题目描述: There is a mysterious lake in the north of Tibet. A ...

随机推荐

  1. Linux下启动和停止Java应用程序的Shell脚本

    转自:http://blog.csdn.net/jadyer/article/details/7960802 资料参考来源自兔大侠,并略作修改:http://www.tudaxia.com/archi ...

  2. jrat

    JRat the Java Runtime Analysis Toolkit What is it? The Java Runtime Analysis Toolkit is a low overhe ...

  3. Oracle错误代码ORA-01653,表空间容量无法扩展

    业务模块在进行增操作时,报错“Caused by: java.sql.BatchUpdateException: ORA-01653: 表 JAZZ_V3.T_MZ_BK 无法通过 128 (在表空间 ...

  4. SSH远程连接连接其他主机,等待时间过长的原因。

    ssh远程连接登录到其他主机,输入登录用户名,等待时间很长时间,然后才出现输入密码的提示.导致这样时间过长,太慢了的原因有两个.(1)当使用ssh远程登录到某个IP时,这个IP的主机系统会读取/etc ...

  5. grep:字符串查找

    转自: http://www.cnblogs.com/peida/archive/2012/12/17/2821195.html Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达 ...

  6. 吐血整理 Delphi系列书籍 118本(全)

    Delphi 教程 系列书籍 网友(老帅)整理 001_<Delhpi6数据库设计思想与实践> 002_<Delphi6应用开发指南> 003_<Delphi6开发人员指 ...

  7. jQuery-实现全选与反选

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 详解java中的数据结构

    线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.本文试图通过简单的描述,向读者阐述各个类的作用以 ...

  9. 新闻焦点切换flash应用

    pixviewer.zip <!-- pixviewer.swf使用--> <script language="javascript" type="te ...

  10. 关于js加密解密

    有的时候有些网站的js用简单的eval混淆加密了.解密其实很简单的 解密JS的eval加密码的方式例如这段: 很多朋友以为这段代码是“加密”的,其实这也谈不上是加密,只能算是一种编码(Encode)或 ...