uva:10487 - Closest Sums(二分查找)
题目大意:给出一组数据,再给出m个查询的数字。
要求找到这组数据里的两个数据相加的和最靠近这个查询的数据,输出那两个数据的和。
解题思路:二分查找。这样找到的话。就输出查询的数值,可是要注意找不到的情况:这里最靠近的值不一定是在找不到的时刻的前一次数据。所以要维护最靠近的要查询数的数值。
代码:
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
using namespace std; const int N = 1005;
int s[N];
int n, q; int min (const int a, const int b) { if (a == 0)
return b;
return abs(a - q) < abs (b - q)? a: b;
} int search () { int l = 0;
int r = n - 1;
int sum = 0;
while (l <= n - 1 && r >= 0 && l < r) { if (s[l] + s[r] < q) { sum = min (sum, s[l] + s[r]);
l++;
}
else if (s[l] + s[r] > q) { sum = min (sum , s[l] + s[r]);
r--;
}
else
return s[l] + s[r];
}
return sum;
} int main () { int m;
int t = 0;
while (scanf ("%d", &n), n) { for (int i = 0; i < n; i++)
scanf ("%d", &s[i]);
sort (s, s + n);
scanf ("%d", &m);
printf ("Case %d:\n", ++t);
for (int i = 0; i < m; i++) { scanf ("%d", &q);
printf ("Closest sum to %d is %d.\n", q, search ());
}
}
return 0;
}
uva:10487 - Closest Sums(二分查找)的更多相关文章
- UVA.10474 Where is the Marble ( 排序 二分查找 )
UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...
- UVa 10539 (筛素数、二分查找) Almost Prime Numbers
题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分 ...
- UVa 1152 和为0的4个值(二分查找)
https://vjudge.net/problem/UVA-1152 题意:给定4个n元素集合A,B,C,D,要求分别从中选取一个元素a,b,c,d,使得a+b+c+d=0.问有多少种取法. 思路: ...
- UVA - 1152 4 Values whose Sum is 0问题分解,二分查找
题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...
- uva-10487 - Closest Sums
暴力枚举后去重最后二分加推断找答案 #include<iostream> #include<map> #include<string> #include<cs ...
- uva11991(二分查找或map的应用)
11991 - Easy Problem from Rujia Liu? Time limit: 1.000 seconds Easy Problem from Rujia Liu? Though R ...
- Python 递归和二分查找
# 二分查找l1 = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88] def two_search( ...
- UVA1152- 枚举 /二分查找
The SUM problem can be formulated as follows: given four lists A,B,C,D of integer values, compute ho ...
- <二分查找+双指针+前缀和>解决子数组和排序后的区间和
<二分查找+双指针+前缀和>解决子数组和排序后的区间和 题目重现: 给你一个数组 nums ,它包含 n 个正整数.你需要计算所有非空连续子数组的和,并将它们按升序排序,得到一个新的包含 ...
随机推荐
- 【BZOJ 4361】 4361: isn (DP+树状数组+容斥)
4361: isn Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 218 Solved: 126 Description 给出一个长度为n的序列A( ...
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树
1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer J ...
- hdu 2795 Billboard 线段树单点更新
Billboard Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=279 ...
- Codeforces Round #245 (Div. 2) A. Points and Segments (easy) 贪心
A. Points and Segments (easy) Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 使用Mysql慢查询日志对有效率问题的SQL进行监控
输入命令:show variables like 'slow%' 可以发现 slow_query_log 为 OFF(默认),表示未开启慢查询日志 slow_query_log_file 为慢查询日志 ...
- DAC calibrates 4- to 20-mA output current
Industrial controls make heavy use of 4- to 20-mA current loops to transmit process measurements bec ...
- FPGA LVDS I/O as an Analog Programmable Comparator
http://www.eetimes.com/author.asp?section_id=36&doc_id=1320289 Seeing the new ADC IP being bandi ...
- mysql字符串比较
select '123'B is TRUE; 1 SET @a='123'; select '123'is TRUE; 0 select cast('222' as ...
- 针对ie的hack
/* 针对ie的hack */ selector { property: value; /* 所有浏览器 */ property: value\9; /* 所有IE浏览器 */ property: v ...