-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (55 loc) · 1.5 KB
/
Copy pathmain.cpp
File metadata and controls
61 lines (55 loc) · 1.5 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
using namespace std;
typedef pair<double, double> Point;
double distance(Point &p1, Point &p2) {
double dx = p1.first - p2.first;
double dy = p1.second - p2.second;
return sqrt(dx*dx + dy*dy);
}
int main(void)
{
vector<Point> v;
set<Point> s;
double x, y;
ifstream pointfile("points.txt");
while (pointfile >> x >> y)
v.push_back(make_pair(x, y));
pointfile.close();
sort(v.begin(), v.end());
double dist = distance(v[0], v[1]);
double D = v[1].first - v[0].first;
int i=0;
for (int j=0; j<v.size(); j++) {
while(v[j].first - v[i].first > D) {
s.erase(make_pair(v[i].second, v[i].first));
i++;
}
s.insert(make_pair(v[j].second, v[j].first));
set<Point>::iterator it;
Point new_point;
it = s.find(make_pair(v[j].second, v[j].first));
it++;
while (it != s.end() && (it->first - v[j].second < D)) {
new_point = make_pair(it->second, it->first);
if (D > distance(new_point, v[j]))
D = distance(new_point, v[j]);
it++;
}
it = s.find(make_pair(v[j].second, v[j].first));
while (it != s.begin() && (v[j].second - it->first < D)) {
it--;
new_point = make_pair(it->second, it->first);
if (D > distance(new_point, v[j]))
D = distance(new_point, v[j]);
}
}
cout << D << "\n";
//for (int i=0; i<v.size(); i++)
// cout << v[i].first << " " << v[i].second << "\n";
return 0;
}