sdut2157 Greatest Number

2024-04-27 02:32
文章标签 number greatest sdut2157

本文主要是介绍sdut2157 Greatest Number,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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<N1000) and M(0<M 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
20 0

示例输出

Case 1: 8


二分查找
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1001005];
int n,m;
int main(){int i,j,cnt,aa;int icase=0;while(scanf("%d%d",&n,&m)&&n!=0&&m!=0){a[0]=0;cnt=1;for(i=1;i<=n;i++){scanf("%d",&aa);if(aa<=m)a[cnt++]=aa;}int cnt1=cnt;for(i=1;i<cnt1;i++)for(j=i;j<cnt1;j++)if(a[i]+a[j]<=m)a[cnt++]=a[i]+a[j];sort(a,a+cnt);int l,r=cnt-1,cur=cnt-1,mid,tmp,maxx=0;for(i=1;i<=cnt;i++){l=i;while(l<=r){mid=(l+r)/2;tmp=a[i]+a[mid];if(tmp>m)r=mid-1;else if(tmp<m){l=mid+1;if(maxx<tmp){cur=mid;maxx=tmp;}}else{maxx=tmp;i=cnt;break;}}r=cur;}printf("Case %d: %d\n",++icase,maxx);printf("\n");}return 0;
} /**************************************Problem id	: SDUT OJ 2157 Result		: Accepted Take Memory	: 2444K Take Time	: 140MS Submit Time	: 2013-05-20 23:40:00  
**************************************/





这篇关于sdut2157 Greatest Number的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/939304

相关文章

usaco 1.2 Name That Number(数字字母转化)

巧妙的利用code[b[0]-'A'] 将字符ABC...Z转换为数字 需要注意的是重新开一个数组 c [ ] 存储字符串 应人为的在末尾附上 ‘ \ 0 ’ 详见代码: /*ID: who jayLANG: C++TASK: namenum*/#include<stdio.h>#include<string.h>int main(){FILE *fin = fopen (

题目1380:lucky number

题目1380:lucky number 时间限制:3 秒 内存限制:3 兆 特殊判题:否 提交:2839 解决:300 题目描述: 每个人有自己的lucky number,小A也一样。不过他的lucky number定义不一样。他认为一个序列中某些数出现的次数为n的话,都是他的lucky number。但是,现在这个序列很大,他无法快速找到所有lucky number。既然

Jenkins 通过 Version Number Plugin 自动生成和管理构建的版本号

步骤 1:安装 Version Number Plugin 登录 Jenkins 的管理界面。进入 “Manage Jenkins” -> “Manage Plugins”。在 “Available” 选项卡中搜索 “Version Number Plugin”。选中并安装插件,完成后可能需要重启 Jenkins。 步骤 2:配置版本号生成 打开项目配置页面。在下方找到 “Build Env

【Hdu】Minimum Inversion Number(逆序,线段树)

利用线段树在nlogn的时间复杂度内求一段数的逆序。 由于给的序列是由0 ~ n -1组成的,求出初始的逆序之后可以递推出移动之后的逆序数。 #include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const in

【JavaScript】基本数据类型与引用数据类型区别(及为什么String、Boolean、Number基本数据类型会有属性和方法?)

基本数据类型   JavaScript基本数据类型包括:undefined、null、number、boolean、string。基本数据类型是按值访问的,就是说我们可以操作保存在变量中的实际的值。 1)基本数据类型的值是不可变的 任何方法都无法改变一个基本类型的值,比如一个字符串: var name = "change";name.substr();//hangconsole.log

ORA-24067: exceeded maximum number of subscribers for queue ADMIN.SMS_MT_QUEUE

临时处理办法: delete from aq$_ss_MT_tab_D;delete from aq$_ss_MT_tab_g;delete from aq$_ss_MT_tab_h;delete from aq$_ss_MT_tab_i;delete from aq$_ss_MT_tab_p;delete from aq$_ss_MT_tab_s;delete from aq$

SQLSERVER排名函数RANK,DENSE_RANK,NTILE,ROW_NUMBER

SQL SERVER排名函数RANK,DENSE_RANK,NTILE,ROW_NUMBER 前言 本文意于用实例数据帮助理解SQL SERVER排名函数RANK,DENSE_RANK,NTILE,ROW_NUMBER。 准备工作 创建测试表:   ? 1 2 3 4 5 create table test( id int identity(1,1)

[LeetCode] 137. Single Number II

题:https://leetcode.com/problems/single-number-ii/ 题目大意 给定array,其中有一个元素只出现了1次,其他元素都出现了3次。 思路 求和 减去 (set(array)*3 - array)/2 作为答案。 class Solution {public int singleNumber(int[] nums) {Set<Long> se

Oracle - ORA-01789: Query block has incorrect number of result columns

一、原因     这个错误一般是在执行表之间的相加(union),相减(minus)等SQL语句时,两个个查询块具有不一致的结果列数所导致的。 二、方案     只要将两段SQL语句的列数调整为一致就可以解决。使用union时,要注意数据库字段的格式要一致,如varchar和nvarchar是不一样的。

Java Number 类和方法

一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte、int、long、double 等。 实例 int a = 5000;float b = 13.65;byte c = 0x4a; 然而,在实际开发过程中,我们经常会遇到需要使用对象,而不是内置数据类型的情形。为了解决这个问题,Java 语言为每一个内置数据类型提供了对应的包装类。 所有的包装类(Integer、Lo