zoukankan      html  css  js  c++  java
  • SDUT ACM 2157 Greatest Number Anti

     

    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
    2
    
    0 0

    示例输出

    Case 1: 8

    //开始用搜索或者顺序查找,都超时,改用二分查找,110ms过了
    #include <stdio.h>
    #include <algorithm>
    int n, m, item = 1;
    int q[1000010], a[1010];
    int main()
    {
    	a[0] = 0;
    	while(scanf("%d %d", &n, &m) != EOF && (n || m))
    	{
    		for(int i = 1; i <= n; i++)
    			scanf("%d", &a[i]);
    		//因为a[0] == 0,所以a[i] + a[j]包括了0个数、1个数、2个数的和
    		std::sort(a, a+n+1);
    		int rear = 0;
    		for(int i = 0; i <= n; i++)
    			for(int j = i; j <= n; j++)//注意j = i;避免重复计算
    				if(a[i] + a[j] <= m)
    					q[rear++] = a[i] + a[j];
    		std::sort(q, q+rear);
    		int ans = 0;
    		for(int i = 0; i < rear; i++)
    		{
    			int left = i, right = rear-1; //同样left = i,避免重复计算
    			while(left < right)
    			{
    				int mid = (left + right) / 2;
    				if(q[i] + q[mid] > m)
    					right = mid - 1;
    				else left = mid + 1;
    			}
    			if(q[i] + q[left] > ans && q[i] + q[left] <= m) //这里要再判断一下是否小于m
    				ans = q[i] + q[left];
    		}
    		printf("Case %d: %d\n\n", item++, ans);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    浅谈Android系统开发中LOG的使用
    在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
    美国地名大全(美国城市名称英文、中文)
    简单工厂模式
    Union和Union All的差别
    strlen和sizeof的差别
    css+div网页设计(一)--基础知识
    Java程序猿面试题集(181- 199)
    【Android XMPP】 学习资料收集贴(持续更新)
    curl命令具体解释
  • 原文地址:https://www.cnblogs.com/wolfred7464/p/3088304.html
Copyright © 2011-2022 走看看