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. c# 类型初始值设定项引发异常

    今天使用VS2010编译c#程序,编译顺利通过,点击运行启动程序,弹框提示如题错误.断点调试,程序甚至都没有进入main函数!!查阅网上资料,几种分析如下(1)反射机制 (2)app.config文件 ...

  2. EditorLineEnds.ttr 受影响的D版本 Delphi 8-2010

    http://stackoverflow.com/questions/25295980/delphi-2006-2010-error-cannot-create-file-c-users-admin- ...

  3. 数据可视化(一)-Matplotlib简易入门

    本节的内容来源:https://www.dataquest.io/mission/10/plotting-basics 本节的数据来源:https://archive.ics.uci.edu/ml/d ...

  4. 《C和指针》读书笔记——第五章 操作符和表达式

    1.当/操作符的两个操作数都是整数时,它执行整除运算:其他都是执行浮点数除法. 2.逻辑移位:左边移入的位用0填充: 算数移位:左边移入的位用符号位填充: 3.位置1 :value |= 1<& ...

  5. ubuntu14.04 中文输入法无法使用

    说下我的解决方法吧,我是忘了在All Settings -> Text Entry 的 Input sources to use中添加Chinese(Pinyin)了,添加后就好了. from: ...

  6. android studio如何开启与禁用版本控制vcs

    1.开启

  7. Linux中Kill进程的N种方法

    常规篇: 首先,用ps查看进程,方法如下: $ ps -ef …… smx       1822     1  0 11:38 ?        00:00:49 gnome-terminal smx ...

  8. windows2003 64位 iis6.0 运行32位web应用程序

    适用于已安装.NET4.0的windows 2003 64位机器. 系统要求: windows 2003 sp1 步骤: 1. 打开命令行,转到目录: %systemdrive%\Inetpub\Ad ...

  9. BitSet

    前几天干了一件比较无聊的事儿——抄了一遍C++ STL bitset的源代码,把不懂的宏定义去掉了,发现(暂时)还能用,嘿嘿. #ifndef BITSET_H #define BITSET_H #i ...

  10. OnDrawGizmos函数

    如果你想绘制可被点选的gizmos,执行这个函数. 这允许你在场景中快速选择重要的物体. 注意: OnDrawGizmos使用相对鼠标坐标 using UnityEngine; using Syste ...