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
uva-10487 - Closest Sums的更多相关文章
- uva:10487 - Closest Sums(二分查找)
题目:10487 - Closest Sums 题目大意:给出一组数据,再给出m个查询的数字. 要求找到这组数据里的两个数据相加的和最靠近这个查询的数据,输出那两个数据的和. 解题思路:二分查找.这样 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
- UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并
UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...
- UVA 10245 - The Closest Pair Problem
Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...
- UVa 11997 K Smallest Sums - 优先队列
题目大意 有k个长度为k的数组,从每个数组中选出1个数,再把这k个数进行求和,问在所有的这些和中,最小的前k个和. 考虑将前i个数组合并,保留前k个和.然后考虑将第(i + 1)个数组和它合并,保留前 ...
- UVA 11997 K Smallest Sums 优先队列 多路合并
vjudge 上题目链接:UVA 11997 题意很简单,就是从 k 个数组(每个数组均包含 k 个正整数)中各取出一个整数相加(所以可以得到 kk 个结果),输出前 k 小的和. 这时训练指南上的一 ...
- 优先队列 UVA 11997 K Smallest Sums
题目传送门 题意:训练指南P189 分析:完全参考书上的思路,k^k的表弄成有序表: 表1:A1 + B1 <= A1 + B2 <= .... A1 + Bk 表2:A2 + B1 &l ...
- UVa 11997 (优先队列 多路归并) K Smallest Sums
考虑一个简单的问题,两个长度为n的有序数组A和B,从每个数组中各选出一个数相加,共n2中情况,求最小的n个数. 将这n2个数拆成n个有序表: A1+B1≤A1+B2≤... A2+B1≤A2+B2≤. ...
随机推荐
- aircrack-ng on OSX 从零开始之探测
继续上一篇内容,在安装好aircrack-ng之后,就要学习如何对目标进行探测了.找了篇教程跟着学习一下吧.其实网上关于使用aircrack-ng的教程还是很多的,我也参考了很多,不过最后还是以官方的 ...
- sharepoint 2013 设置 显示详细错误信息
1. 在当前网站端口目录下的Web.config修改 例如80端口的站点路径为:C:\inetpub\wwwroot\wss\VirtualDirectories\80 (1)将<customE ...
- winform异型不规则界面设计
一,不规则WINFORM窗体 Author:unknown From:Internet在以前版本的Visual Basic或Visual C++中,创建不规则窗体和控件是一件很复杂的事,不仅需要调用大 ...
- 浅析五大ASP.NET数据控件
转自:http://kb.cnblogs.com/page/69207/ 摘要:ASP.NET中有不少的控件,在这当中有一部分是用来处理数据的控件.在这里我们正要讨论的就是ASP.NET数据控件,希望 ...
- 12篇学通C#网络编程
转自:http://www.cnblogs.com/huangxincheng/archive/2012/01/03/2310779.html 在C#的网络编程中,进程和线程是必备的基础知识,同时也是 ...
- C#数据库连接字符串
转自:http://blog.csdn.net/xiaokexinger/article/details/1541441 在MSDN中,.net的数据库连接字符串都有详细的说明,我这里以代码范例的方式 ...
- Square
Square TimeLimit: 1 Second MemoryLimit: 32 Megabyte Totalsubmit: 1638 Accepted: 440 Description ...
- 特性扩展:WebActivatorEx
WebActivator: PreApplicationStartMethod 属性,用于标记作为东西您获取 Web 应用程序启动时调用的方法,在Application_Start前. PostApp ...
- 采集爬虫中,解决网站限制IP的问题? - wendi_0506的专栏 - 博客频道 - CSDN.NET
采集爬虫中,解决网站限制IP的问题? - wendi_0506的专栏 - 博客频道 - CSDN.NET undefined
- eclipse设置项目发布到tomcat webaap下
之前用myeclipse,自动deploy到web app下.目前的项目换了eclipse,项目被发布到本地,通过一个链接到tomcat中,很不习惯. 设置的地方很隐晦,在tomcat中,而不是在pr ...