zoukankan      html  css  js  c++  java
  • 第一届山东ACM-Greatest Number

    Greatest Number

    Description

    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?

     

    Input

    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<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.

    Output

    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.

     

    Sample Input

    2 10
    100
    2
    
    0 0

    Sample Output

    Case 1: 8

    题目大意:给n个数,选择不超过4个数(可以重复)相加,求最大且不大于m的和。
    思路:先构造出一个sum数组,保存num数组中所有情况的0/1/2(每两个数分别相加、一个数加上自身)个数相加。然后再让sum数组两两相加,这样就是不超过4个数相加了,二分查找所得的和中最大且不大于m的(最接近m的)。

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 
     5 #define maxn1 1010
     6 #define maxn2 1000001
     7 
     8 int num[maxn1];
     9 int sum[maxn2];
    10 
    11 int main()
    12 {
    13     int n,m,flag=0,i,j,k;
    14     int lag,ans,l=1,low,high,mid;
    15 
    16     while(scanf("%d%d",&n,&m) != EOF)
    17     {
    18         if(m==0&&n==0)
    19             break;
    20         for(i=1;i<=n;i++)
    21         {
    22             scanf("%d",&num[i]);
    23         }
    24         num[0]=0;//构造出0/1个数相加
    25         k=0;
    26         sort(num,num+n+1);   //比如n=2
    27         for(i=0;i<=n;i++)   //0+0、0+1、0+2;1+1、1+2;2+2
    28         {//这个双重循环包括了0/1/2个数相加
    29             for(j=i;j<=n;j++)
    30             {
    31                 if(num[i]+num[j]<=m)
    32                 {//符合条件的存数sum数组中
    33                     sum[k++]=num[i]+num[j];
    34                 }//这样若sum数组中的两两相加,即<=4个数相加
    35             }
    36         }
    37         sort(sum,sum+k);
    38         lag=0;
    39         ans=0;
    40         for(i=0;i<=k;i++)
    41         {//为了能两两相加,所以有这个循环
    42             low=i;
    43             high=k-1;
    44             while(low<high)
    45             {//二分查找sum数组中两两相加和最接近m的数
    46                 mid=(low+high)/2;
    47                 if(sum[mid]+sum[i]<m)
    48                 {
    49                     low=mid+1;
    50                 }
    51                 else if(sum[mid]+sum[i]==m)
    52                 {
    53                     lag=1;
    54                     break;
    55                 }
    56                 else if(sum[mid]+sum[i]>m)
    57                 {
    58                     high=mid-1;
    59                 }
    60             }
    61             if(lag==1)
    62                 break;
    63             else if(sum[low]+sum[i]>ans&&sum[low]+sum[i]<=m)
    64             {
    65                 ans=sum[low]+sum[i];
    66             }
    67         }
    68         if(flag)
    69             printf("
    ");
    70         if(lag)
    71             printf("Case %d: %d
    ",l++,m);
    72         else
    73             printf("Case %d: %d
    ",l++,ans);
    74         flag=1;
    75     }
    76     return 0;
    77 }
    
    
    
     
  • 相关阅读:
    create-react-app
    简单的PHP的任务队列
    Yii框架中使用PHPExcel导出Excel文件
    Android 使用全局变量的问题
    Android 退出整个应用程序
    new DialogInterface.OnClickListener()报错的解决办法
    Yii 日期时间过滤列 filter
    Yii 时间戳格式化显示的问题
    PullToRefreshListView 应用讲解
    Android:Layout_weight的深刻理解
  • 原文地址:https://www.cnblogs.com/youdiankun/p/3715702.html
Copyright © 2011-2022 走看看