UVA Closest Sums(二分查找)

Problem D

Closest Sums

Input: standard input

Output: standard output

Time Limit: 3 seconds

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.     

Piotr Rudnicki

    题意:n个数据,m次询问,每次输入一个x,询问时输出最靠近x的值y(y是n个数据中任意了两个数的和)

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

int a[10000100];
int b[10001000];
int sum[100100];

int cmp(const void *a,const void *b)
{
    return *(int*)a - *(int*)b;
}

int main()
{
    int n,m;
    int kk = 0;
    while(scanf("%d",&n)!=EOF)
    {
        if(n == 0)
        {
            break;
        }
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        int t = 0;
        for(int i=0; i<n; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                b[t++] = a[i] + a[j];
            }
        }
        qsort(b,t,sizeof(b[0]),cmp);

        sum[0] = b[0];
        int p = 1;
        for(int i=1; i<t; i++)
        {
            if(b[i]!=b[i-1])
            {
                sum[p++] = b[i];
            }
        }

        int T;
        kk++;
        printf("Case %d:\n",kk);
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&m);
            int low = 0;
            int high = p;
            int mid;
            while(low < high)
            {
                mid = (low + high) / 2;
                if(sum[mid] >= m)
                {
                    high = mid;
                }
                else
                {
                    low = mid + 1;
                }
            }

            if(fabs(sum[low-1]-m) < fabs(sum[high]-m) && low>0)
            {
                printf("Closest sum to %d is %d.\n",m,sum[low-1]);
            }
            else
            {
                printf("Closest sum to %d is %d.\n",m,sum[high]);
            }

        }
    }
    return 0;
}
时间: 2024-12-08 07:16:45

UVA Closest Sums(二分查找)的相关文章

uva:10487 - Closest Sums(二分查找)

题目:10487 - Closest Sums 题目大意:给出一组数据,再给出m个查询的数字.要求找到这组数据里的两个数据相加的和最靠近这个查询的数据,输出那两个数据的和. 解题思路:二分查找,这样找到的话,就输出查询的数值,但是要注意找不到的情况:这里最靠近的值不一定是在找不到的时刻的前一次数据,所以要维护最靠近的要查询数的数值. 代码: #include <stdio.h> #include <algorithm> #include <stdlib.h> using

uva 10487 Closest Sums (遍历&amp;二分查找&amp;&amp;双向查找)

题目大意:先给定n个数字,现在要求算出这n个数字的两两之和保存到sum数组,然后在给定m个数,要求找到和每一个数最接近的sum[i]: 挨个计算每个属于其他数之间的sum,然后排序: 查找时有两种方法:二分查找&&双向查找:当然二分查找的效率比后者高了很多,但是都能AC. 提供一条新思路,并不一定非要用二分. 双向查找: #include<stdio.h> #include<algorithm> #include<stdlib.h> using name

UVA - 10487 - Closest Sums (二分求解)

传送:UVA - 10487 10487 Closest Sums 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 c

UVA 10487 Closest Sums(二分)

UVA 10487 Closest Sums 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

UVA10487 Closest Sums【暴力+二分】

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

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.问有多少种取法. 思路:直接暴力枚举的话是会超时的.可以选把a+b的值枚举出来存储,c和d的值也一样并排序,这样就可以在c和d中进行二分查找了. 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int ma

UVa 10539 (筛素数、二分查找) Almost Prime Numbers

题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分查找出小于U和L的最大数的下标,作差即可得到答案. 1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 5 typedef long long LL; 6 7 const int maxn = 10

UVA 10341 (二分查找+精度)

题意: 给你一个关于x的方程,给出变量的值,求出x: Problem F Solve It Input:standard input Output:standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x+ q*sin(x) + r*cos(x) +s*tan(x) +t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of mult

uva 10763 Foreign Exchange(排序+二分查找)

这道题是我第一次算出来应该用什么复杂度写的题,或许是这一章刚介绍过,500000的数据必须用nlogn,所以我就 想到了二分,它的复杂度是logn,再对n个数据进行遍历,正好是nlogn,前两次TLE了,然后我就对我的做法没信心 了...看到一篇博客上说就应该这种方法,然后我就坚定的改自己的算法去了,哈哈,专注度没有达到五颗星,最多 三颗... 思路: 我用的是结构体保存的,先对每一对序列排序,然后对第二个元素在第一个元素中二分搜索,用到了 lower_bound,upper_bound,注释里