-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURI1982-NewComputers.cpp
More file actions
144 lines (120 loc) · 3.29 KB
/
Copy pathURI1982-NewComputers.cpp
File metadata and controls
144 lines (120 loc) · 3.29 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
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
#include <iostream>
#include <vector>
#include <unordered_set>
#include <set>
#include <cmath>
#include <cstdio>
#define int long long
struct Point {
double x;
double y;
Point(double x, double y):x(x),y(y){}
};
bool operator <(const Point& a,const Point& b) {
if (a.x >= 0 && b.x < 0) return true;
if (a.x < 0 && b.x >= 0) return false;
if (a.x == 0 && b.x == 0) {
if (a.y >= 0 || b.y >= 0)
return a.y > b.y;
return b.y > a.y;
}
int det = (a.x) * (b.y) - (b.x) * (a.y);
if (det < 0)
return true;
if (det > 0)
return false;
int d1 = (a.x) * (a.x) + (a.y) * (a.y);
int d2 = (b.x) * (b.x) + (b.y) * (b.y);
return d1 > d2;
}
double get_area(Point a, Point b, Point c) {
double ab = std::sqrt(std::pow((a.x - b.x), 2) + std::pow((a.y - b.y), 2));
double bc = std::sqrt(std::pow((b.x - c.x), 2) + std::pow((b.y - c.y), 2));
double ca = std::sqrt(std::pow((c.x - a.x), 2) + std::pow((c.y - a.y), 2));
double s = (ab + bc + ca) / 2;
return std::sqrt(s * (s - ab) * (s - bc) * (s - ca));
}
int get_distance_from_line(Point a, Point b, Point c) {
return std::abs((c.y - a.y) * (b.x - a.x) - (b.y - a.y) * (c.x - a.x));
}
int get_side(Point a, Point b, Point c) {
int value = (c.y - a.y) * (b.x - a.x) - (b.y - a.y) * (c.x - a.x);
return value == 0 ? 0 : value < 0 ? -1 : 1;
}
void quick_hull(std::vector<Point>& points, Point min, Point max, int side, std::set<Point>& out_hull) {
int max_distance = 0;
Point p = { 0,0 };
bool found = false;
for (int i = 0; i < points.size(); ++i) {
if (get_side(min, max, points[i]) == side) {
int dist = get_distance_from_line(min, max, points[i]);
if (dist > max_distance) {
max_distance = dist;
p = points[i];
found = true;
}
}
}
if (!found) {
out_hull.insert(min);
out_hull.insert(max);
}
else {
int side1 = get_side(p, min, max);
int side2 = get_side(p, max, min);
quick_hull(points, p, min, -side1, out_hull);
quick_hull(points, p, max, -side2, out_hull);
}
}
std::set<Point> convex_hull(std::vector<Point>& points) {
Point min_x = points[0];
Point max_x = points[0];
for (int i = 0; i < points.size();++i) {
if (points[i].x < min_x.x)
min_x = points[i];
if (points[i].x > max_x.x)
max_x = points[i];
}
std::set<Point> hull;
quick_hull(points, min_x, max_x, 1, hull);
quick_hull(points, min_x, max_x, -1, hull);
return hull;
}
#undef int
int main()
{
#define int long long
int N;
while (true) {
std::cin >> N;
if (N == 0) break;
std::vector<Point> points;
double p_x = 0.0;
double p_y = 0.0;
for (int i = 0; i < N;++i) {
double x, y;
std::cin >> x >> y;
p_x += x;
p_y += y;
Point p = {x,y};
points.push_back(p);
}
p_x /= N;
p_y /= N;
for (int i = 0; i < N;++i) {
points[i].x -= p_x;
points[i].y -= p_y;
}
std::set<Point> hull = convex_hull(points);
std::vector<Point> hull_v;
for (auto p : hull) {
hull_v.push_back(p);
}
double per = 0;
for (int i = 0; i < hull_v.size();++i) {
double dist = std::sqrt(std::pow((hull_v[(i+1) % hull_v.size()].y - hull_v[i].y), 2) + std::pow((hull_v[(i+1) % hull_v.size()].x - hull_v[i].x), 2));
per += dist;
}
printf("Tera que comprar uma fita de tamanho %.2f.\n", per - 0.0005);
}
}