Greatest Number

题目描述
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

题目大意:

    输入N和M,N代表下面有N个数,选择其中的1-4个数字,其和与M的差最小,且不大于M。(可重复选择同一个数字)

解题思路:

    现将N个数存入a数组,在将任意两项的和也存入数组a中。此时任取a数组中的2个就组成了所有情况。(1+1=2,1+2=3,2+2=4 ;因为a[0]=0所以存在1+0=1)

    查找的时候使用了二分法,来保证效率,否则超时。

Code:

 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<limits.h>
#define MAXN 1000
using namespace std;
int a[MAXN*MAXN/+*MAXN+];
int main()
{
int i,j;
int n,m,min,resulf,times=;
while (cin>>n>>m)
{
times++;
min=INT_MAX;
if (n==&&m==) break;
for (i=; i<=n; i++)
scanf("%d",&a[i]);
int k=n+;
for (i=; i<=n; i++)
for (j=; j<=i; j++)
a[k++]=a[i]+a[j];
sort(a+,a+k);
for (i=;i<=k-;i++)
{
if (a[i]>m) continue;
int *r=lower_bound(a+,a+k,m-a[i]);
if (m-a[i]-(*r)==) {resulf=m;break;}
if (min>m-a[i]-(*(r-))) {min=m-a[i]-(*(r-));resulf=a[i]+(*(r-));}
}
printf("Case %d: %d\n",times,resulf);
printf("\n");
}
return ;
}

SDUT2157——Greatest Number(STL二分查找)的更多相关文章

  1. STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())

    一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search  -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...

  2. STL二分查找函数的应用

    应用二分查找的条件必须是数组有序! 其中二分查找函数有三个binary_serch,upper_bound,lower_bound 测试数组 int n1[]={1,2,2,3,3,4,5}; int ...

  3. C++ STL 二分查找

    转载自 https://www.cnblogs.com/Tang-tangt/p/9291018.html 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound ...

  4. 10年省赛-Greatest Number (二分+暴力) + 12年省赛-Pick apples(DP) + UVA 12325(暴力-2次枚举)

    题意:给你n个数,在里面取4个数,可以重复取数,使和不超过M,求能得到的最大的数是多少: 思路:比赛时,和之前的一个题目很像,一直以为是体积为4(最多选择四次)的完全背包,结果并不是,两两求和,然后二 ...

  5. POJ 1019:Number Sequence 二分查找

    Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36013   Accepted: 10409 ...

  6. STL 二分查找

    实现源码:https://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 1.在一个递增的数组(或vector)中查找元素属于[ s , ...

  7. STL中的二分查找———lower_bound,upper_bound,binary_search

    关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...

  8. STL中的二分查找

    本文转载于https://blog.csdn.net/riba2534/article/details/69240450 使用的时候注意:必须用在非递减的区间中 二分查找的原理非常简单,但写出的代码中 ...

  9. C++ STL中的Binary search(二分查找)

    这篇博客转自爱国师哥,这里给出连接https://www.cnblogs.com/aiguona/p/7281856.html 一.解释 以前遇到二分的题目都是手动实现二分,不得不说错误比较多,关于返 ...

随机推荐

  1. MySQL EER反向建表

    Database > Synchronize Model... Choose Stored Connection Select the Schemata Choose which to upda ...

  2. node.js:怎样同时执行多条SQLs,且只有一个回调

    本文主要介绍开源node.js库mysql-queries,其可以同时执行多条SQLs,且只有一个回调.同时抛砖引玉,与大家交流node.js开发经验. node.js很大的特点就是事件驱动.非阻塞和 ...

  3. 利用input事件来监听移动端的输入

    今天遇到一个新需求,经理要求评论功能需要限制字数,就像微博那样限制最多输入150字,这里就需要实时提醒用户还能输入多少字了. 在最开始的时候,想到的是监听keyup事件,然后计算用户输入的字数,但是有 ...

  4. CSS3中的变形处理(transform)属性

    在CSS3中,可以利用transform功能来实现文字或图像的旋转.扭曲.缩放.位移.矩阵.原点这六种类型的变形处理,下面将详细讲解transform的使用. 变形--旋转 rotate() div. ...

  5. maven增量编译

    最近由于不清楚maven(2.2.x)增量编译的机制,导致应用出现了一个当时觉得非常诡异的一个问题.先描述一下问题.     背景是应用A有一个公用的base包,版本为1.6.6-SNAPSHOT,应 ...

  6. 非关系型数据库SequoiaDB虚拟机下应用初探

    SequoiaDB是广州巨杉软件有限公司开发的一款新型分布式非关系型数据库.可应用于linux操作系统下.在虚拟机下试用了一下(操作系统Ubuntu),感觉不错,操控简单易上手,在此分享一下心得. 下 ...

  7. javascript dom追加内容的例子

    javascript dom追加内容的使用还是比较广泛的,在本文将为大家介绍下具体的使用方法. 例子: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

  8. linux下rm误删除数据库文件的恢复方法

    在linux redhat 5.4版本,rm误删除数据库文件的恢复过程分享.测试没有问题,可用. 1.首先测试rm 误删除数据库文件 [oracle@primary dbwdn]$ ll total ...

  9. 【转】perl特殊符号及默认的内部变量

    perl特殊符号及默认的内部变量,有需要的朋友不妨参考下 Perl的特殊符号 @       数组                          $x{}   x名字前面是美元符号($),后面是花 ...

  10. 关于分区技术的索引 index

    关于分区技术---索引 Index 一.   分区索引分类: 本地前缀分区索引(local prefixedpartitioned index) 全局分区索引(global partitionedin ...