uva-10487 – Closest Sums

uva-10487 – Closest Sums

大家好,又见面了,我是全栈君。

暴力枚举后去重最后二分加推断找答案

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int count=0;
	int t,m,i,n,j;
	int a[1010];
	while(cin>>n&&n)
	{
		printf("Case %d:\n",++count);
		vector<int>box;
		for(i=0;i<n;i++)
			cin>>a[i];
		for(i=0;i<n;i++)
			for(j=i+1;j<n;j++)
				box.push_back(a[i]+a[j]);
		sort(box.begin(),box.end());
		box.erase(unique(box.begin(),box.end()),box.end());
		cin>>m;
		n=box.size();
		while(m--)
		{
			cin>>t;
			i=lower_bound(box.begin(),box.end(),t)-box.begin();
			if(i==n)
				i--;
			else if(i>0)
				if(abs(box[i]-t)>abs(box[i-1]-t))
					i--;
			printf("Closest sum to %d is %d.\n",t,box[i]);
		}
	}
	return 0;
}

Problem D
Closest Sums
Input: standard input
Output: standard output
Time Limit: 3 seconds

 

Given is a set of integers andthen a sequence of queries. A query gives you a number and asks to find a sum oftwo distinct numbers from the set, which is closest to the query number.

Input

Input contains multiple cases.

Each case starts with an integer n(1<n<=1000), which indicates, how many numbers are in the set of integer.Next n lines contain n numbers. Of course there is only one number in a singleline. The next line contains a positive integer m giving the number ofqueries, 0 < m < 25. The next m lines contain aninteger of the query, one per line.

Input is terminated by a case whose n=0. Surely,this case needs no processing.

Output

Output should be organized as in the samplebelow. For each query output one line giving the query value and the closestsum in the format as in the sample. Inputs will be such that no ties will occur.

Sample input

5

3
12
17
33
34
3
1
51
30
3
1
2
3
3
1
2
3

3

1
2
3
3
4
5
6
0

Sample output

Case 1:
Closest sum to 1 is 15.
Closest sum to 51 is 51.
Closest sum to 30 is 29.
Case 2:
Closest sum to 1 is 3.
Closest sum to 2 is 3.
Closest sum to 3 is 3.
Case 3:
Closest sum to 4 is 4.
Closest sum to 5 is 5.
Closest sum to 6 is 5.


Piotr Rudnicki

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115557.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux下mysql常用命令_shell命令大全

    linux下mysql常用命令_shell命令大全一、总结一下:1.linux下启动mysql的命令:mysqladminstart/ect/init.d/mysqlstart(前面为mysql的安装路径)2.linux下重启mysql的命令:mysqladminrestart/ect/init.d/mysqlrestart(前面为mysql的安装路径)3.linux下关闭mysql的命令:mysqladminshutdown/ec…

    2025年12月8日
    4
  • 《畅玩NAS》家庭 NAS 服务器搭建方案「建议收藏」

    《畅玩NAS》家庭 NAS 服务器搭建方案「建议收藏」NAS(NetworkAttachedStorage:网络附属存储)按字面简单说就是连接在网络上,具备资料存储功能的装置,因此也称为“网络存储器”。它是一种专用数据存储服务器。它以数据为中心,将存储设备与服务器彻底分离,集中管理数据,从而释放带宽、提高性能、降低总拥有成本、保护投资。其成本远远低于使用服务器存储,而效率却远远高于后者。目前国际著名的NAS企业有Netapp、EMC、OUO等。说白话,就是家用的服务器。首选谈谈家庭NAS服务器的基本需求:1.7*24小时运行,最好有UPS电源保护

    2022年6月22日
    81
  • mysql datetime格式化日期(mysql start with)

    *版权声明:本文为博主原创文章,转载请注明出处。*我们在日常数据统计时常会遇到“2018-12-1216:21:12”or“2018-12-1216:21:12.609000”这样的时间格式,假如要统计某一天产生了多少条数据量,SQL该怎么写呢?本文希望能够对大家学习和使用有所帮助。contentcreateTime设备2018-12-1015:20:20…

    2022年4月13日
    51
  • HDU 4859(Bestcoder #1 1003)海岸线(网络流之最小割)[通俗易懂]

    HDU 4859(Bestcoder #1 1003)海岸线(网络流之最小割)

    2022年1月27日
    59
  • Redis事务详解

    Redis事务详解若对事务概念不清楚 请先阅读 彻底理解 MySQL 四种事务隔离级别 这篇文章 链接如下 彻底理解 MySQL 四种事务隔离级别 YaoYong BigData 的博客 CSDN 博客转入正题 结合关系型数据库的事务来看看 Redis 中事务有什么不同 Redis 事务是指将多条命令加入队列 一次批量执行多条命令 每条命令会按顺序执行 事务执行过程中不会受客户端传入的命令请求影响 Redis 事务的相关命令如下 MULTI 标识一个事务的开启 即开启事务 EXEC 执行事务中的所有命令 即提

    2025年10月14日
    4
  • 毕业——少年,你还太弱,请专心练剑

    有段时间没有更新博客,一是比较忙,二是考虑自己的博客内容。之前的博客都是自己的学习记录,输入的同时做了输出,自己思考了一下主要就是以下几类:1、一些比较常见的知识点,像这些都是本来就已经存在的内容,我只是做了一次梳理按照我的逻辑整理出来,就算我不整理,也能找的到,所以我觉得存在的必要性并不大。2、错误记录,自己学习和开发过程中遇到的各种问题,对于遇到类似问题的同学可能有所帮助。3、自己的经验总结,这

    2022年3月11日
    53

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号