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


相关推荐

  • apache配置基于域名访问

    apache配置基于域名访问编辑配置文件,注释掉directory文件,一共四个vim/etc/httpd/conf/httpd.conf然后保存退出检查一下httpd配置文件,ok正常创建虚拟主机,编辑文件路径/etc/httpd/conf.d/a123.confcd/etc/httpd/conf.dvia123.conf然后在a123.conf文件里加入这些东西可直接复制进去其中80是端口DocumentRoot/mnt/z里的/mnt/z是默认主页1.yao.com是域名(如何在主

    2022年7月14日
    10
  • ANSYS ICEM CFD 网格划分步骤简要总结[通俗易懂]

    ANSYS ICEM CFD 网格划分步骤简要总结[通俗易懂]

    2022年5月9日
    36
  • Unity Cinemachine插件全功能详解

    Unity Cinemachine插件全功能详解实现电影级别的分镜,推拉式镜头等,需要2017以上的版本才能使用,配合TimeLine一起使用,和Animator一起.虚拟摄像机不支持AlignwithView【有BUG】还是手动拖比较好1:实现简单的相机跟随效果使用TimeLine实现,由于这次不同于“Unity动画系统案例1”那样需要对人物进行控制。这个项目只是单纯的做CG效果。所以不需要指定动画状态机【但必须挂在Animator…

    2022年6月8日
    56
  • 自学数据挖掘十大算法之AdaBoost「建议收藏」

    自学数据挖掘十大算法之AdaBoost「建议收藏」Adaboost简介:Adaboost(adaptiveboosting)是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器(弱分类器),然后把这些弱分类器集合起来,构成一个更强的最终分类器(强分类器)。其算法本身是通过改变数据分布来实现的,它根据每次训练集之中每个样本的分类是否正确,以及上次的总体分类的准确率,来确定每个样本的权值。该算法其实是一个简单的弱分类算法提升过程

    2022年5月4日
    40
  • 前端调用rpc接口_api接口调用

    前端调用rpc接口_api接口调用问题背景需要根据id通过rpc调用查询具体信息,因为没有提供批量查询的接口,所以做法是挨个遍历查询,那意味着:如果有100个id,就需要顺序进行100次rpc调用,假设每次rpc接口的调用时间是50ms(这个速度很快了),那单单rpc调用就要占用5s,所以接口的响应会非常慢。下面进行优化。优化方案:方案一:让服务方提供批量查询接口,需要服务提供方配合,这里暂不采用。方案二:rpc服务的调用由顺序调用修改为并行调用,采用线程池实现rpc的并发调用。具体实现如下:1)创建线程的类public

    2022年10月11日
    0
  • 忍不住为这样的智慧课堂打call!

    忍不住为这样的智慧课堂打call!

    2022年3月8日
    41

发表回复

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

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