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


相关推荐

  • RDN怎么配置环境

    RDN怎么配置环境非root怎么安装linux版本的lua和torchhttps://blog.csdn.net/BRAVE_NO1/article/details/90260800

    2022年6月18日
    29
  • idea全文搜索快捷键_idea搜索方法快捷键

    idea全文搜索快捷键_idea搜索方法快捷键1、Ctrl+N按名字搜索类相当于eclipse的ctrl+shift+R,输入类名可以定位到这个类文件,就像idea在其它的搜索部分的表现一样,搜索类名也能对你所要搜索的内容多个部分进行匹配,而且如果能匹配的自己写的类,优先匹配自己写的类,甚至不是自己写的类也能搜索。2、Ctrl+Shift+N按文件名搜索文件同搜索类类似,只不过可以匹配所有类型的文件了。3、Ctrl+H查看类的继承关系,例如HashMap的父类是AbstractMap,子类则有一大堆。4、Ctrl+Alt+B查看

    2022年8月30日
    4
  • servlet-Filter过滤器

    servlet-Filter过滤器Filter过滤器Filter过滤器是javaweb的三大组件之一,三大组件分别是:Servlet程序,Listener监听器,Filter过滤器Filter过滤器它是javaEE的规范,也就是接口Filter过滤器它的作用是拦截请求,过滤响应拦截请求常见的应用场景:权限检查日记操作事务管理等等原理package at.guitu.com.FIlter;import javax.servlet.FilterChain;import javax.servlet.Filte

    2022年8月8日
    8
  • CharacterEncodingFilter 详解

    CharacterEncodingFilter 详解一、什么是CharacterEncodingFilter官方解释如下是spring内置过滤器的一种,用来指定请求或者响应的编码格式。在web开发中经常被从来使用二、CharacterEncodingFilter的用法在web.xml中的配置&amp;lt;filter&amp;gt;&amp;lt;filter-name&amp;gt;characterEncodingFilter&amp;lt;/fi…

    2022年7月26日
    20
  • phpstorm2021 激活码【永久激活】

    (phpstorm2021 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlML…

    2022年3月21日
    154
  • MySQL数据库管理常用命令

    MySQL数据库管理常用命令

    2021年8月23日
    50

发表回复

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

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