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.题意:先给定n个数字。在给定m个数字。在m个数字中。找出最接近该数字的 前n的数字中两个数字之和。。
思路:一开始直接暴力。。居然过了。估计数据水不然会超。正确的做法应该是二分查找。(比如m很大的话,每找一个数字都要重新算n *(n-1)/ 2次和,这种操作要进行m次。而用二分的话只需要1次。)。先把每两个数字之和存起来。然后根据输入的数字。用二分查找找到一个最接近的数。。还是要注意细节的地方。因为这个wa了几次。
代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <algorithm>
using namespace std;
int n, m, i, j, sb;
int num[1005];
int sum[500005];
int main() {
int t = 1;
while (~scanf("%d", &n) && n) {
int sumn = 0;
for (i = 0; i < n; i ++) {
scanf("%d", &num[i]);
}
for (i = 0; i < n; i ++)
for (j = i + 1; j < n; j ++) {
if (num[i] != num[j])
sum[sumn ++] = num[i] + num[j];
}
sort(sum, sum + sumn);
scanf("%d", &m);
printf("Case %d:\n", t ++);
while (m --) {
scanf("%d", &sb);
int sbb;
if (sb >= sum[sumn - 1]) {sbb = sumn - 1;}
else if (sb <= sum[0]) {sbb = 0;}
else {
int star = 0, end = sumn - 1;
while (end != star) {
sbb = (star + end) / 2;
if (sum[sbb] < sb) {
if (sbb == star) break;
star = sbb;
}
else if (sum[sbb] > sb) {
if (sbb == end) break;
end = sbb;
}
else
break;
}
}
if (abs(sum[sbb - 1] - sb) < abs(sb - sum[sbb]) && sbb != 0)
sbb --;
if (abs(sum[sbb + 1] - sb) < abs(sb - sum[sbb]) && sbb != sumn - 1)
sbb ++;
printf("Closest sum to %d is %d.\n", sb, sum[sbb]);
}
}
return 0;
}
附上一开始暴力水过的代码- - 代码上是简单了不少。。而且居然跑得更快。。无解。估计是n大的数据里面。m都很小甚至为1吧。。。哎
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <algorithm>
using namespace std;
int n, m, i, j, sb, sbb;
int num[1005];
int minn;
int sum;
int main() {
int t = 1;
while (~scanf("%d", &n) && n) {
int sumn = 0;
for (i = 0; i < n; i ++) {
scanf("%d", &num[i]);
}
scanf("%d", &m);
printf("Case %d:\n", t ++);
while (m --) {
scanf("%d", &sb);
int minn = 999999999;
for (i = 0; i < n; i ++)
for (j = i + 1; j < n ; j ++) {
sum = num[i] + num[j];
if (abs(sum - sb) < minn) {
minn = abs(sum - sb);
sbb = sum;
}
}
printf("Closest sum to %d is %d.\n", sb, sbb);
}
}
return 0;
}