Uva--10487(二分)

2014-07-23 20:49:56

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 mlines 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

思路:经典二分,每两个数相加,排序,二分,一气呵成。

 1 /*************************************************************************
 2     > File Name: Uva10487.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Tue 22 Jul 2014 10:43:36 PM CST
 6  ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 int n,m,q,v[1005],cnt,cnt2,ans,Case = 0;
17 int val[1000005];
18 
19 int main(){
20    while(scanf("%d",&n) == 1 && n){
21        printf("Case %d:\n",++Case);
22        for(int i = 0; i < n; ++i)
23            scanf("%d",&v[i]);
24        sort(v,v + n);
25        cnt = unique(v,v + n) - v;
26        cnt2 = 0;
27        for(int i = 0; i < cnt; ++i){
28            for(int j = i + 1; j < cnt; ++j){
29                 val[cnt2++] = v[i] + v[j];
30            }
31        }
32        sort(val,val + cnt2);
33        scanf("%d",&m);
34        int tmin,ans;
35        while(m--){
36            tmin = 1000000000;
37            scanf("%d",&q);
38            int mid,l = 0,r = cnt2;
39            while(l < r){
40                mid = l + (r - l) / 2;
41                if(abs(val[mid] - q) < tmin){
42                     tmin = abs(val[mid] - q);
43                     ans = val[mid];
44                 }
45                if(val[mid] == q) break;
46                else if(val[mid] > q) r = mid;
47                else l = mid + 1;
48            }
49            printf("Closest sum to %d is %d.\n",q,ans);
50        }
51    }
52    return 0;
53 }
posted @ 2014-07-23 20:52  Naturain  阅读(126)  评论(0)    收藏  举报