UVA10487(二分)
Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two 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 single line. The next line contains a positive integer m giving the number of queries, 0 < m < 25. The next m lines contain an integer 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 sample below. For each query output one line giving the query value and the closest sum 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.
题意:给一个数和一组数列,在数列选两个数组合出距离这个数最近的值。
收获:了解了unique函数。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 500
vector<int> a;
vector<int> a1;
vector<int> a2;
int n;
int main()
{
int cas = ;
while(~scanf("%d", &n)&&n)
{
a.clear();
a1.clear();
int b;
for(int i = ; i < n; i++)
{
scanf("%d", &b);
a.push_back(b);
}
for(int i = ; i < a.size(); i++)
for(int j = i + ; j < a.size(); j++)
a1.push_back(a[i]+a[j]);
sort(a1.begin(), a1.end());
// a2.clear();
// a2.push_back(a1[0]);
// for(int i = 1; i < a1.size(); i++) if(a1[i] != a1[i-1]) a2.push_back(a1[i]);
vector<int>::iterator iter = unique(a1.begin(), a1.end());
a1.erase(iter, a1.end());
int m;
printf("Case %d:\n", cas++);
scanf("%d", &m);
int c;
for(int i = ; i < m; i++)
{
scanf("%d", &c);
vector<int>::iterator it;
it = lower_bound(a1.begin(), a1.end(), c);
if(*it == c) printf("Closest sum to %d is %d.\n", c, c);
else
{
int ans = *it;
if(it != a1.begin() && abs(*(it-) - c) < abs(*it - c))
ans = *(it - );
printf("Closest sum to %d is %d.\n", c, ans); }
}
}
return ;
}
UVA10487(二分)的更多相关文章
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]
2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 3352 Solved: 919[Submit][Stat ...
- 整体二分QAQ
POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...
- [bzoj2653][middle] (二分 + 主席树)
Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- jvascript 顺序查找和二分查找法
第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...
- BZOJ 1305: [CQOI2009]dance跳舞 二分+最大流
1305: [CQOI2009]dance跳舞 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...
- BZOJ 3110 [Zjoi2013]K大数查询 ——整体二分
[题目分析] 整体二分显而易见. 自己YY了一下用树状数组区间修改,区间查询的操作. 又因为一个字母调了一下午. 貌似树状数组并不需要清空,可以用一个指针来维护,可以少一个log 懒得写了. [代码] ...
随机推荐
- ps&&/proc/pid/xxx
ps 如果想看一个进程的启动时间,可以用lstart来看 [root@jiangyi02.sqa.zmf /home/ahao.mah] #ps -eo pid,lstart,etime,cmd |g ...
- ListView之BaseAdapter
BaseAdapter可以实现自定义的丰富子项视图,本文实现如下所示结果: 实现代码: /* ListView :列表 BaseAdapter 通用的基础适配器 * * */ public class ...
- JavaScript 基础一
内部: <Script Language="JavaScript" type="text/javascript"> JavaScript代码 < ...
- InnoDB和MyISAM存储引擎的区别
在MySQL数据库的使用过程中我们经常会听到存储引擎这个名词.MySQL的存储引擎有好多种如InnoDB.MyISAM.Memory.NDB等等,多存储引擎也是MySQL数据库的特色. InnoDB和 ...
- 虚拟Linux 訪问win7共享文件夹方法
虚拟机訪问win7的共享文件夹 首先安装增强功能,这个不用多说 再者选择菜单中的设备->共享目录,设置为固定分配和自己主动挂载 在终端敲入命令df:发现有自己创建共享的文件夹 然后运行例如以下命 ...
- 前端判断用户请求是PC还是移动端
链接:https://www.zhihu.com/question/20004700/answer/13678113 第一步先在服务器端使用User Agent判断,先匹配出移动设备,这一步可以统计U ...
- UML学习-时序图
时序图(Sequence Diagram)是显示对象之间交互的图,这些对象是按时间顺序排列的.顺序图中显示的是参与交互的对象及其对象之间消息交互的顺序.时序图中包括的建模元素主要有:对象(Actor) ...
- Android-------手机屏幕适配之文件适配
public class Main { //定义文件本地存储路径,可按照需求更改 private final static String rootPath = &quo ...
- SQL Server -SET NOCOUNT
SET NOCOUNT 使返回的结果中不包含有关受 Transact-SQL 语句影响的行数的信息. 语法 SET NOCOUNT { ON | OFF } 注释 当 SET NOCOUNT 为 ON ...
- alarm函数可以定时
貌似是可以的,不过感觉好像这样用不是很好,最好还是用回timer_settimer一些列函数吧,不过既然开了头,就看下alarm怎么用吧. 1. 所需头文件 #include<unistd.h ...