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


相关推荐

  • 阿里云windows server 安装软件_阿里云windows

    阿里云windows server 安装软件_阿里云windows在阿里云服务器上安装svn与在本地安装是一样的,详细步骤参考楼主之前的帖子http://blog.csdn.net/m0_37027631/article/details/60773156楼主一开始安装完后,访问服务端svn连接失败。原因是因为阿里云服务器中的80、443端口没有开放的原因。如何开放阿里云服务器中的端口请参考http://blog.csdn.net/m0_3702763

    2022年10月10日
    4
  • Scala 专题指南

    Scala 专题指南

    2022年1月7日
    37
  • 电子信息系统机房设计规范 GB50174-2017

    电子信息系统机房设计规范 GB50174-2017一、物理安全1.1物理安全主要包括:(1)机房环境安全(2)通信线路安全(3)设备安全(4)电源安全1.1.1机房的安全等级分为三个基本类别:A类:对计算机机房的安全有严格的要求,有完

    2022年7月2日
    32
  • Linux内存映射——mmap

    Linux内存映射——mmap一mmap系统调用1.内存映射所谓的内存映射就是把物理内存映射到进程的地址空间之内,这些应用程序就可以直接使用输入输出的地址空间,从而提高读写的效率。Linux提供了mmap()函数,用来映射物理内存。在驱动程序中,应用程序以设备文件为对象,调用mmap()函数,内核进行内存映射的准备工作,生成vm_area_struct结构体,然后调用设备驱动程序中定义的mmap函数。2.

    2022年6月16日
    50
  • django配置文件详解_pycharm配置django

    django配置文件详解_pycharm配置django前言django框架的日志通过python内置的logging模块实现的,既可以记录自定义的一些信息描述,也可以记录系统运行中的一些对象数据,还可以记录包括堆栈跟踪、错误代码之类的详细信息。log

    2022年7月30日
    11
  • 贵金属黄金投资基础知识及技术交易篇「建议收藏」

    贵金属黄金投资基础知识及技术交易篇「建议收藏」贵金属黄金投资基础知识及技术交易篇  一、影响贵金属价格波动的因素  我们通常所说的贵金属主要指黄金和白银,由于它们的属性有很多共同点,所以价格走势基本相同,影响价格的因素也大同小异,皇玛hmcfds贵金属交易平台小编讲讲贵金属交易的基础知识:  1、供需关系:任何商品价格的波动本质因素都是供需关系的变化,贵金属也不例外。如果产量大幅增加就可能导致贵金属价格的下跌,如果投资交易、实物首…

    2022年5月8日
    44

发表回复

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

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