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. android非法字符的判定、表情符号的判定

    public class EmojiEditText extends EditText {// 输入表情前的光标位置private int cursorPos; // 输入表情前EditText中的文 ...

  2. Android内存优化12 内存泄漏常见情况3 注册泄漏

    android 中有很多注册和反注册,由于在注册后,上下文自身会被持久化的观察者列表所持有,如果不进行反注册,就会造成内存泄漏 内存泄漏1:Sensor Manager 代码如下: MainActiv ...

  3. 搭建开源入侵检测系统Snort并实现与防火墙联动

    Snort作为一款优秀的开源主机入侵检测系统,在windows和Linux平台上均可安装运行.BT5作为曾经的一款经典的渗透神器,基于 Ubuntu,里面已经预装很多的应用,比如Mysql.Apach ...

  4. 转: RSA原理 阮一峰的博客

    转:http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html 讲的非常细致,易懂.

  5. 【转】maven常见问题问答

    转自:http://www.iteye.com/topic/973166 前言 Maven,发音是[`meivin],"专家"的意思.它是一个很好的项目管理工具,很早就进入了我的必 ...

  6. Unity3D入门基础之游戏对象 (GameObject) 和组件 (Component) 的关系

    原文出处:http://edu.china.unity3d.com/learning_document/getData?file=/Manual/TheGameObject-ComponentRela ...

  7. android精确绘制文字位置的方法

    android 中使用Canvas的drawText绘制文本的位置,是基于基线的. 例如以下图: 当中字母Q的小尾巴在横线以下了. 怎么样找准字母的中心位置呢? 先看以下的样例:(右边的数字,表示字体 ...

  8. VS报表图解《一》---菜鸟版

    与原先的开发环境VB.EXE不同VS2013自带了报表控件ReportViewer能够内部实现报表的设计,本文主要通过绑定数据集来实现报表的显示 1.加入:reportviewer控件,当将控件显示在 ...

  9. 解决ios7.0 以后自己定义导航栏左边button靠右的问题

    1.自己定义button //左button UIButton *leftBtn = [[UIButton , , , )]; [leftBtn addTarget:self action:@sele ...

  10. C#中怎样获取当前路径的几种方法

    String apppath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase; //获取整个文件路径名 a ...