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


相关推荐

  • 寒假33天读书笔记

    寒假33天读书笔记整理一下电脑资料 无意发现大二年寒假的一段读书笔记 距离现在快三年了 再回头浏览一下 感觉那会上大学时候还真是蛮自由 想读什么就读什么 不像现在出来工作后 读书已经带有很明确的目的性 记得那学期选修了 美学概论 本来是只想混个学分的 上课都是带着 thinkinjava 过去 结果被老师 记不得老师名字了 只记得是人文管理学院的老师 一讲 从西方地理环境扯到文化起源和发展 再对比东方文化

    2025年10月3日
    4
  • ico图标在谷歌浏览器中如何显示?

    ico图标在谷歌浏览器中如何显示?

    2021年10月9日
    184
  • 常见Android面试题及答案(详细整理)

    常见Android面试题及答案(详细整理)以下是一些常用的Android面试题及答案,有需要没需要都可以备着,希望能帮到大家。 1.请描述一下Activity生命周期。答:如下图所示。共有七个周期函数,按顺序分别是:onCreate(),onStart(),onRestart(),onResume(),onPause(),onStop(),onDestroy()。onCreate():创建Activit…

    2022年5月21日
    58
  • SQL Server和MySQL锁定提示比较

    SQL Server和MySQL锁定提示比较

    2021年9月17日
    55
  • IE8允许ActiveX控件设置

    IE8允许ActiveX控件设置IE11允许ActiveX控件设置在IE浏览器菜单栏,找到“Internet选项”,—》“安全”—》“Internet”—》“自定义级别”找到ActiveX控件相关信息。其他都改为启用对未标记为可安全执行脚本的ActiveX控件初始化并执行脚本:提示下载未签名的ActicveX控件:提示下载已签名的ActicveX控件:提示允许script默认原设置

    2022年5月14日
    54
  • python编程是啥-Python编程「建议收藏」

    python编程是啥-Python编程「建议收藏」随着人工智能时代的来临,编程语言的热度居高不下,随着智能机器人的出现,在2020年更是迎来一波新的高峰。在这样的大环境下,关于谁是人工智能最流行编程语言的讨论也十分热烈,C、C++、Java、Python等编程语言不断被拿出来做比较。那么接下来,风变编程来解析一波当下编程语言的新趋势。Python受欢迎度有望超越Java?近日,TIOBE编程语言社区公布了2020年10月编程语言排行榜。榜单数据显…

    2022年5月17日
    35

发表回复

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

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