码迷,mamicode.com
首页 > 其他好文 > 详细

UVA Closest Sums(二分查找)

时间:2015-02-02 18:15:34      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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;
}



UVA Closest Sums(二分查找)

标签:

原文地址:http://blog.csdn.net/yeguxin/article/details/43409643

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!