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)
上一篇 2022年2月2日 下午8:00
下一篇 2022年2月2日 下午8:00


相关推荐

  • 从零开始写一个发送h264的rtsp服务器(上)「建议收藏」

    从零开始写一个发送h264的rtsp服务器(上)

    2022年3月13日
    105
  • Python递归实现全排列

    Python递归实现全排列排列:从n个元素中任取m个元素,并按照一定的顺序进行排列,称为排列;全排列:当n==m时,称为全排列;比如:集合{1,2,3}的全排列为:{123} {132}{213}{231}{321}{312}递归思想:取出数组中第一个元素放到最后,即a[1]与a[n]交换,然后递归求a[n-1]的全

    2022年6月29日
    42
  • NYOJ 12 喷水装置(二)「建议收藏」

    NYOJ 12 喷水装置(二)

    2022年1月20日
    54
  • 微信公众平台开发入门教程[2020版]

    微信公众平台开发入门教程[2020版]在这篇微信公众平台开发教程中,我们假定你已经有了PHP语言程序、MySQL数据库、计算机网络通讯、及HTTP/XML/CSS/JS等基础。我们将使用微信公众账号方倍工作室作为讲解的例子,二维码见左侧。本系列教程将引导你完成如下任务:创建新浪云计算平台应用 启用微信公众平台开发模式 体验常用接收消息及发送消息类型 了解数据收发原理及消息格式第一章申请服务器资源创建新浪云计算应用申请账号我们使用SAE新浪云计算平台作为服务器资源,并且申请PHP环境+MySQL数据库作为程.

    2022年6月6日
    259
  • IDEA/Pycharm的config目录以及插件的安装位置在哪里?

    IDEA/Pycharm的config目录以及插件的安装位置在哪里?文章目录在 2020 版前在 2020 版后在 2020 版前通过 IDEA properties 文件可知 config 目录 system 目录 插件安装目录都在用户目录下插件安装目录就在 config 目录下 在 2020 版后把 pycharm 和 IDEA 的 config 等目录都放在了这个路径下面 直接进去 IDEA 的目录 就相当于之前的 config 目录

    2026年3月27日
    2
  • Spring Cloud面试题(2020最新版)[通俗易懂]

    Spring Cloud面试题(2020最新版)[通俗易懂]文章目录为什么需要学习SpringCloud什么是SpringCloud设计目标与优缺点设计目标优缺点SpringCloud发展前景整体架构主要项目SpringCloudConfigSpringCloudNetflixSpringCloudBusSpringCloudConsulSpringCloudSecuritySpringCloudSleuthSpringCl…

    2022年5月7日
    66

发表回复

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

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