-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
199 lines (163 loc) · 3.54 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// 51-closest-pair.cpp : Defines the entry point for the console application.
//
#include <vector>
#include <string>
#include <fstream>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cctype>
//#include <cfenv>
using std::begin;
using std::end;
using Coordinate = std::pair<long long int, long long int>;
using CoordinateVector = std::vector <Coordinate> ;
double squared_distance(const Coordinate &a, const Coordinate &b)
{
//std::feclearexcept(FE_ALL_EXCEPT);
double dx = static_cast<double>(a.first - b.first);
double dy = static_cast<double>(a.second - b.second);
double dx2 = dx * dx;
double dy2 = dy * dy;
return dx2 + dy2;
//if (std::fetestexcept(FE_OVERFLOW))
// return std::numeric_limits<double>::max();
//else
// return dist2;
}
void subtract_smallest(CoordinateVector &coordinates)
{
Coordinate smallest;
smallest.first = std::numeric_limits<long long int>::max();
smallest.second = std::numeric_limits<long long int>::max();
for (const Coordinate &c : coordinates)
{
if (c.first < smallest.first)
smallest.first = c.first;
if (c.second < smallest.second)
smallest.second = c.second;
}
for (Coordinate &c : coordinates)
{
c.first -= smallest.first;
c.second -= smallest.second;
}
}
double process(const CoordinateVector &coordinates)
{
double shortest_distance = std::numeric_limits<double>::max();
CoordinateVector::const_iterator a = begin(coordinates);
CoordinateVector::const_iterator b = a;
b++;
while (true)
{
while (b != end(coordinates))
{
double distance = squared_distance(*a, *b);
if (distance < shortest_distance)
shortest_distance = distance;
b++;
}
a++;
b = a;
b++;
if (b == end(coordinates))
break;
}
return std::sqrt(shortest_distance);
}
Coordinate parse_coord(const std::string &text)
{
auto num_spaces = std::count_if(begin(text), end(text), [](char c)
{
return std::isblank(c);
});
assert(num_spaces == 1);
Coordinate ret;
size_t space = text.find(" ");
assert(space != text.npos);
if (num_spaces == 1 && space != text.npos)
{
ret.first = atoll(text.substr(0, space).c_str());
ret.second = atoll(text.substr(space).c_str());
}
return ret;
}
bool is_number(const std::string &s)
{
return !s.empty() && std::find_if(s.begin(), s.end(), [](char c)
{
return !std::isdigit(c);
}) == s.end();
}
bool read_next_set(std::ifstream &fin, CoordinateVector &coords)
{
if (fin.good())
{
std::string line;
std::getline(fin, line);
bool valid = is_number(line);
assert(valid);
if (valid)
{
int n = std::atoi(line.c_str());
if (n == 0)
return false;
if (n > 0)
{
coords.clear();
coords.reserve(n);
while (fin.good())
{
assert(n > 0);
std::getline(fin, line);
auto c = parse_coord(line);
coords.push_back(c);
n--;
if (n == 0)
return true;
}
assert(false);
return false;
}
else
{
assert(false);
return false;
}
}
else
{
assert(false);
return false;
}
}
else
{
assert(false);
return false;
}
}
int main(int argc, char *argv[])
{
if (argc > 1)
{
std::string filename(argv[1]);
std::ifstream fin(filename.c_str());
if (fin.is_open())
{
CoordinateVector coordinates;
while (read_next_set(fin, coordinates))
{
subtract_smallest(coordinates);
double distance = process(coordinates);
if (distance < 10000.f)
std::cout << std::fixed << std::setprecision(4) << distance << "\n";
else
std::cout << "INFINITY\n";
}
}
}
return 0;
}