-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBest Position for a Service Centre.cpp
49 lines (43 loc) · 1.71 KB
/
Best Position for a Service Centre.cpp
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
// Runtime: 99 ms (Top 45.86%) | Memory: 22.5 MB (Top 13.53%)
class Solution {
public:
const double MIN_STEP = 0.000001; // With 0.00001 not AC
const int dx[4] = {0, 0, 1,-1};
const int dy[4] = {1, -1, 0, 0};
double totalDist(vector<vector<int>>& positions, double cx, double cy) {
double dist = 0;
for (auto p : positions) {
dist += hypot(p[0] - cx, p[1] - cy);
}
return dist;
}
double getMinDistSum(vector<vector<int>>& positions) {
int n = (int)positions.size();
double cx = 0, cy = 0;
for (auto p : positions) { cx += p[0], cy += p[1]; }
cx /= n, cy /= n;
pair<double, double> minDistCenter = {cx, cy};
double minDist = totalDist(positions, cx, cy);
//printf("cx = %.4lf, cy = %.4lf, minDist = %.4lf\n", minDistCenter.first, minDistCenter.second, minDist);
double step = 50.0; // Because max value of x, y could be 100. So half of that
while (step > MIN_STEP) {
pair<double, double> tempCenter = minDistCenter;
double tempDist = minDist;
for (int k = 0; k < 4; k++) {
double xx = minDistCenter.first + dx[k] * step;
double yy = minDistCenter.second + dy[k] * step;
double d = totalDist(positions, xx, yy);
//printf("d = %.4lf\n", d);
if (d < minDist) {
tempCenter = {xx, yy};
tempDist = d;
}
}
if (minDistCenter == tempCenter) step /= 2;
minDistCenter = tempCenter;
minDist = tempDist;
}
//printf("minDist = %.4lf\n", minDist);
return minDist;
}
};