zoukankan      html  css  js  c++  java
  • 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<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

    提示

     

    来源

     2010年山东省第一届ACM大学生程序设计竞赛
     

    分析

    开始看题想用深搜,用了很长时间剪枝,最后还是超时,发现是算法的问题,后来看到有解题报告用二分法做的,恍然大悟。
    算法不同,用二分查找即使不用多余的剪枝也不会超时,效率差太多(同学说得好:遍历就应该想到二分查找)

    此题题意:为求从n个数中任选不超过四个数相加,让和不超过已知数m的最大值,并输出此最大值

    不超过四个,就是说一个,两个,三个和四个这四种可能。不难得出,只要求出任意两数之和,就可组合出三数之和和四数之和。
    用二分超找效率极高。

    令a[0]=0,在二分查找中便可以有一个,两个,三个和四个数的和。

    代码

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cmath>
     6 
     7 using namespace std;
     8 int a[1000010];
     9 int main()
    10 {
    11     int n,m,c=1;
    12     while(scanf("%d %d",&n,&m)!=EOF)
    13     {
    14         if(n==0&&m==0)
    15             break;
    16         int ans=0,falg=1;
    17         memset(a,0,sizeof(a));
    18         for(int i=1;i<=n;i++)//存入单个数
    19             scanf("%d",&a[i]);
    20         int len=n;
    21         for(int i=1;i<=n;i++)//补充每两个数的和
    22             for(int j=i;j<=n;j++)
    23                 a[++len]=a[i]+a[j];
    24         sort(a,a+len+1);//len为非零的长度   +1为a[0]=0
    25         
    26         for(int i=0;i<=len&&falg;i++)
    27         {
    28             int s=i,e=len;
    29             //二分查找
    30             while(s<=e)
    31             {
    32                 int mid=(s+e)/2;
    33                 if(a[i]+a[mid]==m)
    34                 {
    35                     falg=0;
    36                     ans=m;
    37                     break;
    38                 }
    39                 else if(a[i]+a[mid]>m)
    40                     e=mid-1;
    41                 else
    42                 {
    43                     s=mid+1;
    44                     if(a[i]+a[mid]>ans)
    45                         ans=a[i]+a[mid];
    46                 }
    47 
    48             }
    49         }
    50         printf("Case %d: %d
    
    ",c++,ans);
    51     }
    52 }
    快速代码

    知道算法后快速敲的,只考虑代码简洁性,反正AC了

    考虑运行过程的话应该还可以再简化,大家自己看着改吧

     
  • 相关阅读:
    uC/OS-II时间(OS_time)块
    uC/OS-II任务(OS_task)块
    uC/OS-II信号(OS_sem)块
    uC/OS-II队列(OS_q)块
    uC/OS-II互斥信号(OS_mutex)块
    uC/OS-II内存(OS_mem)块
    elasticsearch-installation
    rabbitmq的安装
    str_翻转字符串
    str_2.判断两个字符串是否互为旋转词
  • 原文地址:https://www.cnblogs.com/vivider/p/3662436.html
Copyright © 2011-2022 走看看