-
Notifications
You must be signed in to change notification settings - Fork 11
/
SPOJ18531.cc
98 lines (90 loc) · 2.19 KB
/
SPOJ18531.cc
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
// SPOJ 18531: Barrelrider
// http://www.spoj.com/problems/BRLRIDER/
//
// Solution: geometry (tangent of circle)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <complex>
using namespace std;
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
// arg gives [-pi, pi] value.
double EPS = 1e-8;
int sign(double x) {
if (x < -EPS) return -1;
if (x > +EPS) return +1;
return 0;
}
typedef complex<double> point;
struct circle { point p; double r; };
// Let q be a tangent point.
// The angle between q-p-c.p is
// sin(t) = r/|p - c.p|.
// and the solution is
// p + (c.p - p) * exp(\pm it).
vector<point> tangent(point p, circle c) {
double sin2 = c.r*c.r/norm(p - c.p);
if (sign(1-sin2) < 0) return {};
if (sign(1-sin2) == 0) return {p};
point z(sqrt(1-sin2), sqrt(sin2));
return {p+(c.p-p)*conj(z), p+(c.p-p)*z};
}
const double PI = 4*atan(1.0);
// measure of union of x[j].
template <class T>
T covered_range(vector<pair<T, T>> x) {
typedef pair<T, int> event;
vector<event> es;
for (int i = 0; i < x.size(); ++i) {
es.push_back({x[i].fst, i});
es.push_back({x[i].snd,~i});
}
sort(all(es));
int c = 0;
T a = es[0].fst, ans = 0;
for (auto e: es) {
if (c > 0) ans += e.fst - a;
if (e.snd >= 0) ++c;
else --c;
a = e.fst;
}
return ans;
}
void doit(int n) {
vector<circle> cs;
for (int i = 0; i < n; ++i) {
double x, y, r;
scanf("%lf %lf %lf", &x, &y, &r);
cs.push_back({point(x,y), r});
}
point o(0,0);
vector<pair<double ,double>> obstacles;
for (int i = 0; i < n; ++i) {
vector<point> ts = tangent(o, cs[i]);
if (ts.size() == 0) {
printf("100%\n");
return;
}
if (ts.size() == 1) {
point u = (cs[i].p - o) * point(0,-1);
point v = (cs[i].p - o) * point(0,+1);
ts = {u, v};
}
double a1 = arg(ts[0]);
double a2 = arg(ts[1]);
if (a1 >= a2) {
obstacles.push_back({a1, PI});
obstacles.push_back({-PI, a2});
} else {
obstacles.push_back({a1, a2});
}
}
double cov = covered_range(obstacles);
printf("%.0f%\n", 100.0 * cov / (2*PI));
}
int main() {
int n; scanf("%d", &n); doit(n);
}