SDUT2157——Greatest Number(STL二分查找)
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二分查找)的更多相关文章
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...
- STL二分查找函数的应用
应用二分查找的条件必须是数组有序! 其中二分查找函数有三个binary_serch,upper_bound,lower_bound 测试数组 int n1[]={1,2,2,3,3,4,5}; int ...
- C++ STL 二分查找
转载自 https://www.cnblogs.com/Tang-tangt/p/9291018.html 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound ...
- 10年省赛-Greatest Number (二分+暴力) + 12年省赛-Pick apples(DP) + UVA 12325(暴力-2次枚举)
题意:给你n个数,在里面取4个数,可以重复取数,使和不超过M,求能得到的最大的数是多少: 思路:比赛时,和之前的一个题目很像,一直以为是体积为4(最多选择四次)的完全背包,结果并不是,两两求和,然后二 ...
- POJ 1019:Number Sequence 二分查找
Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36013 Accepted: 10409 ...
- STL 二分查找
实现源码:https://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 1.在一个递增的数组(或vector)中查找元素属于[ s , ...
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- STL中的二分查找
本文转载于https://blog.csdn.net/riba2534/article/details/69240450 使用的时候注意:必须用在非递减的区间中 二分查找的原理非常简单,但写出的代码中 ...
- C++ STL中的Binary search(二分查找)
这篇博客转自爱国师哥,这里给出连接https://www.cnblogs.com/aiguona/p/7281856.html 一.解释 以前遇到二分的题目都是手动实现二分,不得不说错误比较多,关于返 ...
随机推荐
- UVaLive 7361(矩阵快速幂)
题意:矩阵快速幂求斐波那契数列. 分析:
- item44:将与参数无关的代码抽离template
编写non-template代码中,重复十分明显:可以很直观的看到代码的重复,然后将它们写成一个新的class或者函数,然后供调用. 编写template代码中,重复是隐晦的:只存在一份templat ...
- 关键字 extern
定义:extern可置于变量或者函数前,以表示变量或者函数的定义在别的文件中.编译器会到其他模块中寻找其定义. extern int f(); extern int i; extern关键字 作为 ...
- 再说 extern "C"
早知道 C++ 源文件中要调用C语言函数需要在函数申明时 指定extern "C": 要不然可以编译通过,但连接时提示找不到什么什么符号,原因是C和C++生成的函数名不一样,ext ...
- Win7设置共享文件夹
在系统中,实现共享的前提是,双方要在同一个局域网内,同一个工作组或者域内. 添加共享用户 点击确定即可,最好把共享的权限设置为只读类型,方便管理. 可以使用计算机的用户名或者IP地址来访问 如果还是不 ...
- js 的数据类型转换
一直对js的类型转换一直半解,今天理一下思路,首先说一下几个特殊的数值 null null是特殊的object,故 typeof null 返回object, null派生于undefined ,故 ...
- Json对象序列化与反序列化
如果后台的参数数对象,需要在前台传入: JS代码: //创建JS对象 var CUTTING_TABLET_MO = new Object(); CUTTING_TABLET_MO.CUTTING_T ...
- C# Json数据反序列化为Dictionary并根据关键字获取指定值
Json数据: { "dataSet": { "header": { "returnCode": "0", " ...
- VB6-操作数据库
平常搞数据库操作多了就想把经常用的内容放在一起,我也懒,在一本书里的工程例子挑了一个bas,修修改改,凑合这用吧. Public strCnn As String '数据库连接字符串 Public A ...
- ASP.NET生命周期事件顺序
普通页面运行规律 Page_PreInitPage_InitPage_InitCompletePage_PreLoadPage_LoadButton1事件触发!Page_LoadCompletePag ...