forked from improper4/uva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa00109_SCUDBusters.java
212 lines (169 loc) · 4.61 KB
/
UVa00109_SCUDBusters.java
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 45 (109 - SCUD Busters) */
/* SUBMISSION: 09305546 */
/* SUBMISSION TIME: 2011-09-27 15:22:11 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
import java.awt.*;
public class UVa00109_SCUDBusters {
static int signedTriangleArea(Point a, Point b, Point c) {
return a.x * b.y - a.y * b.x + a.y * c.x -
a.x * c.y + b.x * c.y - c.x * b.y;
}
static boolean ccw(Point a, Point b, Point c) {
return signedTriangleArea(a, b, c) > 0;
}
static boolean collinear(Point a, Point b, Point c) {
return signedTriangleArea(a, b, c) == 0;
}
static double distance(Point p1, Point p2) {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return Math.sqrt(dx * dx + dy * dy);
}
static Polygon graham(Polygon poly) {
int N = poly.npoints;
int[] x = poly.xpoints;
int[] y = poly.ypoints;
Point[] in = new Point[N];
for (int i = 0; i < N; ++i)
in[i] = new Point(x[i], y[i]);
final Point first;
// Choose the point with the least y coordinate, or least x coordinate in case of a tie
int min = 0;
for (int i = 1; i < N; ++i) {
if (in[i].y < in[min].y)
min = i;
else if (in[i].y == in[min].y)
if (in[i].x < in[min].x)
min = i;
}
first = in[min];
in[min] = in[0];
in[0] = first;
// Sort by angle with first
Arrays.sort(in, 1, N, new Comparator<Point>() {
public int compare(Point p1, Point p2) {
if (collinear(first, p1, p2))
return Double.compare(distance(first, p1), distance(first, p2));
if (ccw(first, p1, p2))
return -1;
else
return 1;
}
});
Stack<Point> S = new Stack<Point>();
S.push(in[0]);
S.push(in[1]);
for (int i = 2; i < N; ++i) {
while (S.size() > 1) {
Point top = S.pop();
Point nextTop = S.pop();
S.push(nextTop);
S.push(top);
if (!ccw(nextTop, top, in[i]))
S.pop();
else
break;
}
S.push(in[i]);
}
int M = S.size();
int[] hullx = new int[M];
int[] hully = new int[M];
int k = M - 1;
while (!S.isEmpty()) {
Point q = S.pop();
hullx[k] = q.x;
hully[k] = q.y;
k--;
}
return new Polygon(hullx, hully, M);
}
static double area(Polygon poly) {
int N = poly.npoints;
int[] x = poly.xpoints;
int[] y = poly.ypoints;
Point[] p = new Point[N];
for (int i = 0; i < N; ++i)
p[i] = new Point(x[i], y[i]);
final Point first;
int min = 0;
for (int i = 1; i < N; ++i)
if (p[i].y < p[min].y)
min = i;
else if (p[i].y == p[min].y)
if (p[i].x < p[min].x)
min = i;
first = p[min];
p[min] = p[0];
p[0] = first;
Arrays.sort(p, 1, N, new Comparator<Point>() {
public int compare(Point p1, Point p2) {
if (collinear(first, p1, p2))
return Double.compare(distance(first, p1), distance(first, p2));
if (ccw(first, p1, p2))
return -1;
else
return 1;
}
});
double A = 0.0;
for (int i = 0; i < N; ++i) {
int j = (i + 1) % N;
A += p[i].x * p[j].y - p[j].x * p[i].y;
}
return A / 2.0;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Polygon> polygons = new ArrayList<Polygon>();
while (true) {
int N = Integer.parseInt(in.readLine());
if (N == -1)
break;
int[] x = new int[N];
int[] y = new int[N];
for (int i = 0; i < N; ++i) {
StringTokenizer stk = new StringTokenizer(in.readLine());
x[i] = Integer.parseInt(stk.nextToken());
y[i] = Integer.parseInt(stk.nextToken());
}
polygons.add(new Polygon(x, y, N));
}
double total = 0.0;
int M = polygons.size();
double[] areas = new double[M];
boolean[] visited = new boolean[M];
for (int i = 0; i < M; ++i) {
Polygon hull = graham(polygons.get(i));
areas[i] = area(hull);
polygons.set(i, hull);
}
for (int i = 0; i < M; ++i) {
}
String line;
StringTokenizer stk;
while ((line = in.readLine()) != null) {
stk = new StringTokenizer(line);
int qx = Integer.parseInt(stk.nextToken());
int qy = Integer.parseInt(stk.nextToken());
Point q = new Point(qx, qy);
for (int i = 0; i < M; ++i) {
Polygon p = polygons.get(i);
if (p.contains(q)) {
if (!visited[i]) {
total += areas[i];
visited[i] = true;
}
break;
}
}
}
System.out.printf(Locale.ENGLISH, "%.2f%n", total);
in.close();
System.exit(0);
}
}