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≤. ...
随机推荐
- Leaflet学习笔记-基础内容
为什么选择Leaflet 开源,且代码仅有 31 KB,但它具有开发人员开发在线地图的大部分功能(80%的功能) 是不是比arcgis要小很多呢 官网:http://leafletjs.com/ 劣势 ...
- 23个.NET开源项目
Castle是.NET里走过了三年的开源框架,下载地址如:http://www.castleproject.org/index.html ,当然如果你是从事过JAVA开发并用过spring,hiber ...
- jquery - 通过点击切换文字内容
今天要写一个简单的显示/隐藏效果,本以为是挺简单的事儿,没想到还真因为基本功不扎实遇到了问题,这里跟大家分享一下. 百度了很多方法,精简能用的干货实在太少,最后还是通过去查jq的官方api才找到了解决 ...
- nginx日志配置
nginx日志配置 http://www.ttlsa.com/linux/the-nginx-log-configuration/ 日志对于统计排错来说非常有利的.本文总结了nginx日志相关的配置如 ...
- windows笔记-一个简单的windows GUI应用程序
#include<windows.h> // 编写Windows程序必须包含的头文件 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-006-当构造函数有集合时的注入
一.当构造函数有集合时,只能用<CONSTRUCTOR-ARG>,不能用C-NAMESPACE 二. 1. package soundsystem.collections; import ...
- 更好的 SQL 模式的 10 条规则
更好的 SQL 模式的 10 条规则 2015-06-17 11:57:392353浏览1评论 在创建新表和数据仓库时,要做很多决定.一些在当时似乎无关紧要的地方,却让你和用户在数据库的生命期内感到痛 ...
- 【HDOJ】2707 Steganography
简单字符串,读懂题,很容易AC. #include <stdio.h> #include <string.h> ], line[], des[]; int main() { i ...
- pcDuino 刷系统-LiveSuit
准备: pcduino : 点此购买 支持HDMI的显示器:点此购买 或参考无显示器刷机与使用.至少1张4G microSD卡,如果内存卡不大,可以用内存卡刷内核,用u盘刷系统 LiveSuit ...
- jQuery对象和DOM对象原来不一样啊
jQuery对象和DOM对象使用说明,需要的朋友可以参考下.1.jQuery对象和DOM对象第一次学习jQuery,经常分辨不清哪些是jQuery对象,哪些是 DOM对象,因此需要重点了解jQuery ...