UVa 10487 Closest Sums:遍历&二分查找

10487 - Closest Sums

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428

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 integerm 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.

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

思路:可以直接O(N^2)枚举最接近的数,但是不妨排序后,对每个数a[i],在a[i+1]~a[n-1]之间二分查找与a[i]之和最接近query的数,复杂度O(Nlog N)

完整代码:

/*0.012s*/

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define sf scanf
#define pf printf
const int INF=0x3f3f3f3f;  

int a[1005];
int cas = 0, n, m, query, closest, diff, i, j;  

inline void find()
{
    if (j == n || j != i + 1 && diff - a[j - 1] < a[j] - diff)
    {
        if (diff - a[j - 1] < abs(closest - query))
            closest = a[i] + a[j - 1];
    }
    else
    {
        if (a[j] - diff < abs(closest - query))
            closest = a[i] + a[j];
    }
}  

int main()
{
    while (sf("%d", &n), n)
    {
        for (i = 0; i < n; ++i)
            sf("%d", &a[i]);
        sort(a, a + n);
        sf("%d", &m);
        pf("Case %d:\n", ++cas);
        while (m--)
        {
            sf("%d", &query);
            closest = INF;
            for (i = 0; i < n - 1; ++i)
            {
                diff = query - a[i];
                j = lower_bound(a + i + 1, a + n, diff) - a;
                find();
            }
            pf("Closest sum to %d is %d.\n", query, closest);
        }
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索input
, query
, number
, and
, closest
closest()
10487、iso 10487、closest、jquery closest、jq closest,以便于您获取更多的相关知识。

时间: 2024-08-03 02:49:34

UVa 10487 Closest Sums:遍历&amp;二分查找的相关文章

uva 10487 - Closest Sums

点击打开链接 题目意思:    先给定n个数字,现在要求算出这n个数字的两两之和保存到sum数组,然后在给定m个数,要求找到和每一个数最接近的sum[i],输出 解题思路:    1:二分查找                       2:由于数据很大,所以直接硬搞肯定是不行的,那么我们选择二分查找.首先先求出这些元素组成的所有的和,保存到数组sum里面,然后对sum排序,最后进行二分查找,找到距离目标最近的sum[i]输出即可 代码: #include <algorithm> #inclu

UVa 10125 Sumsets (折半枚举&amp;amp;二分查找)

10125 - Sumsets Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1066 Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S

UVa 10474 Where is the Marble? (二分查找&amp;amp;equal_range()的使用

10474 - Where is the Marble? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=1415 Raju and Meena love to play with Marbles. They have got a lot of marble

UVa 10487:Closest Sums

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428 原题: 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 di

【C/C++学院】0723-32位与64位/调戏窗口程序/数据分离算法/内存检索/二分查找法/myVC

[送给在路上的程序员] 对于一个开发者而言,能够胜任系统中任意一个模块的开发是其核心价值的体现. 对于一个架构师而言,掌握各种语言的优势并可以运用到系统中,由此简化系统的开发,是其架构生涯的第一步. 对于一个开发团队而言,能在短期内开发出用户满意的软件系统是起核心竞争力的体现. 每一个程序员都不能固步自封,要多接触新的行业,新的技术领域,突破自我. 32位与64位 地址与内存的关系 4G = 4*1024M = 4*1024*1024k = 4*1024*1024*1024 Byte字节 = 2

在MySQL中实现二分查找的详细教程_Mysql

给定一个升序排列的自然数数组,数组中包含重复数字,例如:[1,2,2,3,4,4,4,5,6,7,7].问题:给定任意自然数,对数组进行二分查找,返回数组正确的位置,给出函数实现.注:连续相同的数字,返回第一个匹配位置还是最后一个匹配位置,由函数传入参数决定. 我为什么会出这道题目?     二分查找在数据库内核实现中非常重要     在数据库的内核实现中,二分查找是一个非常重要的逻辑,几乎99%以上的SQL语句(所有索引上的范围扫描/等值查询/Unique查询等),都会使用到二分查找进行数据的

C语言编程中实现二分查找的简单入门实例_C 语言

架设有一个数组 v 已经按升序排列了,数组 v 有 n=20 个元素.数组中有个元素 x,如何知道 x 位于该数组的第几位呢? 解决这个问题的一个普遍方法就是二分查找法.下面是程序: #include <stdio.h> int binsearch(int x, int v[], int n); main() { int i, result, n; int wait; int x = 17; // 需要查找的数值 int v[19]; // 定义一个数组 // 给数组赋值 for(i = 0;

[算法]二分查找算法

[思想] 二分搜索主要解决的问题是确定排序后的数组x[0,n-1]中是否包含目标元素target. 二分搜索通过持续跟踪数组中包含元素target的范围(如果target存在数组中的话)来解决问题. 一开始,这个范围是整个数组,然后通过将target与数组中的中间项进行比较并抛弃一半的范围来缩小范围.该过程持续进行, 直到在数组中找到target或确定包含target的范围为空时为止.在有n个元素的表中,二分搜索大约需要执行lgn次比较操作. 提供充足的时间,竟然只有10%的专业程序员能够将这个

Python实现二分查找算法实例

  本文实例讲述了Python实现二分查找算法的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #!/usr/bin/env python import sys def search2(a,m): low = 0 high = len(a) - 1 while(low <= high): mid = (low + high)/2 midval = a[mid] if midval <