-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvexHull.java
More file actions
89 lines (76 loc) · 2.57 KB
/
Copy pathConvexHull.java
File metadata and controls
89 lines (76 loc) · 2.57 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
import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
// acmicpc
class Solver {
BufferedWriter bw;
int T;
int N, M, K;
int MAX = (int) 1e9;
ArrayList<Point> A;
Solver() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
T = Integer.parseInt(st.nextToken());
for (int t = 0; t < T; t++) {
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
ArrayList<Point> A = new ArrayList<Point>();
Point p = new Point(MAX, MAX);
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
A.add(new Point(x, y));
if (x < p.x || (x == p.x && y < p.y)) {
p = new Point(x, y);
}
}
Point pp = p;
Collections.sort(A, (a, b) -> {
long val = ccw(pp, a, b);
if (val > 0)
return -1;
else if (val < 0)
return 1;
return Long.compare(dist(pp, a), dist(pp, b));
});
Point[] S = new Point[N];
int top = 0;
for (int i = 0; i < N; i++) {
Point cur = A.get(i);
while (top > 1 && ccw(S[top - 2], S[top - 1], cur) <= 0) {
--top;
}
S[top++] = cur;
}
// for (int i = 0; i < top; i++) {
// System.out.println(S[i]);
// }
// S[] 에 있는 애들이 convex hull
// top: size of S
bw.write(top + "\n");
}
bw.flush();
bw.close();
}
private long dist(Point p, Point a) {
long dx = a.x - p.x;
long dy = a.y - p.y;
return dx * dx + dy * dy;
}
private long ccw(Point p, Point a, Point b) {
return ccw(new Point(a.x - p.x, a.y - p.y), new Point(b.x - p.x, b.y - p.y));
}
private long ccw(Point a, Point b) {
return (long) a.x * b.y - (long) b.x * a.y;
}
}