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;
    }
    

      

  • 相关阅读:
    App唤起微信小程序和回调
    微信小程序 — 自定义picker选择器弹窗内容+textarea穿透bug
    微信小程序的场景值scene
    微信小程序textarea层级过高(盖住其他元素)
    微信小程序如何修改本地缓存key中的单个数据
    微信小程序---查看更多的显示与隐藏
    微信小程序文字超过行后隐藏并且显示省略号
    Flutter 页面下拉刷新和上拉加载
    json转换成dart类 JSON to Dart
    Flutter 保持页面状态
  • 原文地址:https://www.cnblogs.com/wolfred7464/p/3088304.html
Copyright © 2011-2022 走看看