-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVa-10487.cpp
More file actions
67 lines (55 loc) · 1.47 KB
/
UVa-10487.cpp
File metadata and controls
67 lines (55 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h> // here we have all the STL we need, including istringstream and ostringstream
#define ALL(x) x.begin(), x.end()
#define FAST std::cin.tie(0); ios::sync_with_stdio(false); std::cout.tie(0);
using namespace std;
unordered_set<int> s;
int arr[1000000];
int findClosest(int arr[], int n, int target);
#define min(a,b) (a < b) ? (a) : (b)
// can be solved in a more efficent way. (try it again when you are stronger).
int main() {
int n;
int num;
int m;
int tc = 1;
while (scanf("%d", &n) && n) {
s.clear();
memset(arr, 2147483646, sizeof(arr));
while (n--) {
scanf("%d", &num);
s.insert(num);
}
int i = 0;
for (auto y : s) {
for (auto t : s) {
if (y != t) {
arr[i++] = y + t;
}
}
}
sort(arr, arr+1000000);
scanf("%d", &m);
int lower, low1, low2;
printf("Case %d:\n", tc);
while (m--) {
scanf("%d", &num);
int o = findClosest(arr, 1000000, num);
printf("Closest sum to %d is %d.\n", num, o);
}
tc++;
}
}
int findClosest(int arr[], int n, int target)
{
int left = 0, right = n - 1;
while (left < right) {
if (abs(arr[left] - target)
<= abs(arr[right] - target)) {
right--;
}
else {
left++;
}
}
return arr[left];
}