-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP109.java
321 lines (287 loc) · 8.2 KB
/
P109.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import java.awt.Color;
import java.awt.Polygon;
import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
//import viewers.GraphView;
public class P109 {
public static void main(String[] args) throws IOException {
// Read kingdoms:
List<Kingdom> kingdoms = new LinkedList<Kingdom>();
// GraphView gv = new GraphView();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = br.readLine()) != null) {
final int N = Integer.parseInt(line);
if(N == -1)
break;
// Build kingdom of size N:
Point[] buildings = new Point[N];
for(int i = 0; i < N; ++i) {
String[] parts = br.readLine().split("\\s+");
buildings[i] = new Point(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
// gv.addPoint(new Point2D.Double(buildings[i].x, buildings[i].y), Color.GREEN);
}
kingdoms.add(new Kingdom(buildings));
}
{
double sizeSum = 0;
for(Kingdom k : kingdoms) {
// System.err.println(" Wall with " + k.wall.points.length + " points with area "+ k.getArea());
sizeSum += k.getArea();
// gv.addPolygon(k.getPolygon());
}
// System.err.println("Total kingdom area: " + sizeSum);
}
//*/
// Read missiles:
double unlitArea = 0;
while((line = br.readLine()) != null) {
String[] parts = line.split("\\s+");
if(parts.length < 2)
break;
Point missile = new Point(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
Kingdom ruined = null;
for(Kingdom k : kingdoms) {
if(k.isHitByMissile(missile)) {
//System.err.println("HIT! " + missile + " -> " + k);
ruined = k;
break;
}
}
if(ruined != null) {
//gv.addPoint(new Point2D.Double(missile.x, missile.y), Color.RED);
unlitArea += ruined.getArea();
kingdoms.remove(ruined);
}
else {
// System.err.println("MISS! " + missile);
//gv.addPoint(new Point2D.Double(missile.x, missile.y), Color.BLUE);
}
}
System.out.printf("%.2f\n", unlitArea);
// gv.show();
}
}
class Kingdom {
ConvexHull wall;
public Kingdom(Point[] buildings) {
wall = new ConvexHull(buildings);
}
public boolean isHitByMissile(Point missile) {
return wall.contains(missile);
}
public double getArea() {
return wall.getArea();
}
public String toString() {
return "Kingdom with wall of " + wall.points.length + " points and area " + getArea();
}
public Polygon getPolygon() {
int[] xs = new int[wall.points.length];
int[] ys = new int[wall.points.length];
for(int i = 0; i < xs.length; ++i) {
xs[i] = wall.points[i].x;
ys[i] = wall.points[i].y;
}
return new Polygon(xs, ys, xs.length);
}
}
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point flipY() {
return new Point(x, -y);
}
public static boolean leftTurn(Point lineStart, Point lineEnd, Point p) {
return (lineEnd.x-lineStart.x)*(p.y-lineStart.y) - (lineEnd.y-lineStart.y)*(p.x-lineStart.x) > 0;
}
public static boolean rightTurn(Point lineStart, Point lineEnd, Point p) {
return (lineEnd.x-lineStart.x)*(p.y-lineStart.y) - (lineEnd.y-lineStart.y)*(p.x-lineStart.x) < 0;
}
public static boolean colinear(Point lineStart, Point lineEnd, Point p) {
return (lineEnd.x-lineStart.x)*(p.y-lineStart.y) - (lineEnd.y-lineStart.y)*(p.x-lineStart.x) == 0;
}
public boolean equals(Point other) {
return x == other.x && y == other.y;
}
public String toString() {
return "(" + x + "," + y + ")";
}
}
/*
* Convex hull represented by a list of points in clockwise order.
*/
class ConvexHull {
Point[] points;
public ConvexHull(Point[] scatteredPoints) {
// System.err.println("Initiating construction of convex hull with " + scatteredPoints.length + " points");
// Sort input by x:
Arrays.sort(scatteredPoints, new Comparator<Point>() {
public int compare(Point a, Point b) {
return a.x-b.x;
}
});
// Find left and rightmost points:
Point leftMost = scatteredPoints[0];
Point rightMost = scatteredPoints[scatteredPoints.length-1];
//Split points in upper and lower:
List<Point> upperPoints = new LinkedList<Point>();
List<Point> lowerPoints = new LinkedList<Point>();
for(int i = 1; i < scatteredPoints.length-1; ++i) {
Point p = scatteredPoints[i];
if(Point.colinear(leftMost, rightMost, p))
continue;
if(Point.leftTurn(leftMost, rightMost, p))
upperPoints.add(p);
else
lowerPoints.add(p.flipY());
}
// new AsciiPrinter(Arrays.asList(scatteredPoints), upperPoints, flipY(lowerPoints)).print();
//Construct upper hull:
upperPoints = upperHull(leftMost, rightMost, upperPoints);
//Construct lower hull:
lowerPoints = upperHull(leftMost.flipY(), rightMost.flipY(), lowerPoints);
/*
System.err.println("Points on hulls:");
System.err.println(" Upper hull: " + upperPoints);
System.err.println(" Lower hull: " + lowerPoints);
new AsciiPrinter(Arrays.asList(scatteredPoints), upperPoints, flipY(lowerPoints)).print();
assert upperPoints.get(0).equals(lowerPoints.get(0).flipY());
*/
//Make points of hull:
points = new Point[upperPoints.size() + lowerPoints.size()];
int i = 0;
for(Point p : upperPoints) {
points[i++] = p;
}
points[i] = rightMost;
i = points.length;
// System.err.println("CH:" + Arrays.asList(points));
for(Point p : lowerPoints) {
i--;
if(i != points.length-1)
points[i+1] = p.flipY();
}
// System.err.println("CH:" + Arrays.asList(points));
// new AsciiPrinter(Arrays.asList(scatteredPoints), Arrays.asList(points)).print();
}
private List<Point> flipY(List<Point> l) {
LinkedList<Point> res = new LinkedList<Point>();
for(Point p : l) {
res.add(p.flipY());
}
return res;
}
private List<Point> upperHull(Point left, Point right, List<Point> between) {
//System.err.println("Constructing CH of " + left + "->" + between + "->" + right);
Stack<Point> out = new Stack<Point>();
between.add(right);
out.add(left);
Point lastPeek = right;
for(Point p : between) {
if(p.x == lastPeek.x) {
if(p.y < lastPeek.y) {
continue;
}
else {
lastPeek = out.pop();
}
}
//System.err.println(" " + out + "->" + lastPeek);
while(!out.isEmpty() && !Point.rightTurn(out.peek(), lastPeek, p)) {
lastPeek = out.pop();
}
out.push(lastPeek);
lastPeek = p;
}
// Fix rightmost point as it should be handled differently:
out.push(lastPeek);
if(out.peek() == right) {
out.pop();
}
//System.err.println(" " + out + "->" + lastPeek);
return out;
}
public boolean contains(Point p) {
Point last = points[points.length-1];
for(Point point : points) {
if(point.equals(p))
return true;
}
for(Point point : points) {
if(!Point.rightTurn(last, point, p))
return false;
last = point;
}
return true;
}
public double getArea() {
int res = 0;
Point prev = points[points.length-1];
for(Point p : points) {
res += prev.x*p.y- prev.y*p.x;
prev = p;
}
return res*-0.5;
}
}
class AsciiPrinter {
private List<Point> [] data;
public AsciiPrinter(List<Point> ...lists) {
data = lists;
}
public void print() {
// Find bounding box:
StringBuilder sb = new StringBuilder();
int minx = Integer.MAX_VALUE;
int miny = Integer.MAX_VALUE;
int maxx = Integer.MIN_VALUE;
int maxy = Integer.MIN_VALUE;
for(List<Point> list : data) {
for(Point p : list) {
minx = Math.min(minx, p.x);
maxx = Math.max(maxx, p.x);
miny = Math.min(miny, p.y);
maxy = Math.max(maxy, p.y);
}
}
if(maxx-minx > 80)
return;
// Construct lines:
int pointsPrLine = maxx-minx+1;
int lines = maxy-miny+1;
byte[] screenBuffer = new byte[lines*pointsPrLine];
byte id = 1;
for(List<Point> list : data) {
for(Point p : list) {
screenBuffer[pointsPrLine*(p.y-miny)+(p.x-minx)] = id;
if(data.length == 1)
++id;
}
++id;
}
// Write lines:
for(int y = maxy; y >= miny; --y) {
for(int x = minx; x <= maxx; ++x) {
id = screenBuffer[pointsPrLine*(y-miny)+(x-minx)];
if(id == 0)
sb.append(' ');
else
sb.append((char)('A' + (id - 1)));
}
sb.append('\n');
}
System.err.print(sb);
}
}