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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 大数据云计算和物联网之间的区别和联系_云计算和大数据的区别

    大数据云计算和物联网之间的区别和联系_云计算和大数据的区别一、概念1.云计算“云”指通过计算机池提供资源,也就是说,不是构建一两台机器的问题,而是要构建一定规模的集群,并且对该集群统一管理,形成”资源池“,才能满足云计算业务的需求。简单地说,**云计算就是基于互联网将规模化资源池的计算、存储、平台开发和软件能力提供给用户,实现自动化、低成本、快速提供和灵活伸缩的IT服务。**云计算代表了以虚拟化技术为核心、以低成本为目标的、动态可扩展的…

    2022年10月7日
    5
  • 什么是网站的灵魂_测试性能网站

    什么是网站的灵魂_测试性能网站前言在前一篇随笔《大型网站系统架构的演化》中,介绍了大型网站的演化过程,期间穿插了一些技术和手段,我们可以从中看出一个大型网站的轮廓,但想要掌握设计开发维护大型网站的技术,需要我们一步一步去研究实践

    2022年8月4日
    5
  • 数据挖掘项目的生命周期是指_数据挖掘生命周期的数据准备阶段

    数据挖掘项目的生命周期是指_数据挖掘生命周期的数据准备阶段 第一步:数据收集; 第二步:数据清理和转换;          目前有很多技术能用于数据清理和转换:           1、数据类型转换;          2、连续列转换;          3、分组;          4、聚集;          5、缺失值处理;          6、删除孤立点; 第三步:模型构建 

    2025年9月14日
    6
  • python中变量的命名以及使用[通俗易懂]

    python中变量的命名以及使用[通俗易懂]一、变量的概念变量名只有在第一次出现的时候,才是定义变量。当再次出现时,不是定义变量,而是直接使用之前定义的变量。1.变量命名1)命名的规范性变量名可以包括字母、数字、下划线,但是数字不能做为开头。例如:name1是合法变量名,而1name就不可以。 系统关键字不能做变量名使用 除了下划线之个,其它符号不能做为变量名使用 Python的变量名是除分大小写的2)驼峰命名法…

    2022年6月18日
    33
  • navicatpremium15激活码(JetBrains全家桶)

    (navicatpremium15激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月21日
    121
  • preparedStatement和Statement区别「建议收藏」

    preparedStatement和Statement区别「建议收藏」原文:https://blog.csdn.net/xuebing1995/article/details/72235380  一、概念PreparedStatement是用来执行SQL查询语句的API之一,Java提供了Statement、PreparedStatement和CallableStatement三种方式来执行查询语句,其中Statement用于通用查询,Prep…

    2022年6月4日
    33

发表回复

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

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