-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem68.cpp
More file actions
58 lines (45 loc) · 1.2 KB
/
problem68.cpp
File metadata and controls
58 lines (45 loc) · 1.2 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
#include <bits/stdc++.h>
using namespace std;
class WinterHeater {
vector<int> houses;
vector<int> heaters;
public:
void readInput() {
string line;
int x;
// Read houses
cout << "houses = ";
getline(cin, line);
istringstream hs(line);
while (hs >> x)
houses.push_back(x);
// Read heaters
cout << "heaters = ";
getline(cin, line);
istringstream ht(line);
while (ht >> x)
heaters.push_back(x);
}
int findMinRadius() {
sort(houses.begin(), houses.end());
sort(heaters.begin(), heaters.end());
int radius = 0;
for (int house : houses) {
auto it = lower_bound(heaters.begin(), heaters.end(), house);
int dist1 = INT_MAX, dist2 = INT_MAX;
if (it != heaters.end()) dist1 = abs(*it - house);
if (it != heaters.begin()) dist2 = abs(*prev(it) - house);
radius = max(radius, min(dist1, dist2));
}
return radius;
}
void display() {
cout << findMinRadius() << "\n";
}
};
int main() {
WinterHeater wh;
wh.readInput();
wh.display();
return 0;
}