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. Go -- FileManage 自建云盘

    一.介绍 Caddy,用Go写的一款相当优秀的Web服务器软件,它有不少很有特色的功能,国内目前来说用的不多,不过也逐渐有越来越多的人知道了,它有个特色的插件功能,其中一款插件是FileManager ...

  2. 如何实现jenkins的多平台任务同时执行

    如果需要我们的程序在多个平台(linux.mac.window)同时执行,该如何操作 1.首先需要构建一个“多配置项目” 2.配置项目在过个平台上运行

  3. iOS:多线程的详细介绍

    多线程: 一.概念 1.什么是进程?     程序的一次性执行就是进程.进程占独立的内存空间.   2.什么是线程?     进程中的代码的执行路径.   3.进程与线程之间的关系?      每个进 ...

  4. Android 将ARGB图片转换为灰度图

    思路如下: 1.读取or照相,得到一张ARGB图片. 2.转化为bitmap类,并对其数据做如下操作: A通道保持不变,然后逐像素计算:X = 0.3×R+0.59×G+0.11×B,并使这个像素的值 ...

  5. AWR报告简易分析

    Snap Id Snap Time Sessions Cursors/Session Begin Snap: 35669 2012-11-8 13:00 1246 11.3 End Snap: 356 ...

  6. 过滤xss攻击和sql注入函数

    /**+----------------------------------------------------------* The goal of this function is to be a ...

  7. MySql Replication基本原理

    Replication的思想是将数据在集群的多个节点同步.备份,以提高集群数据的可用性(HA):Mysql使用Replication架构来实现上述目的,同时可以提升了集群整体的并发能力.5.6版本作为 ...

  8. metal 优化数据分析

    https://developer.apple.com/documentation/metal/render_pipeline/viewing_pipeline_statistics_of_a_dra ...

  9. 报错:configure: error: no acceptable C compiler found in $PATH

    运行以下命令报错: ./configure 错误: checking whether to enable maintainer-specific portions of Makefiles... ye ...

  10. 2018.1.9 博客迁移至csdn

    http://blog.csdn.net/liyuhui195134?ref=toolbar