
2μ°¨μ νλ©΄ μμ Nκ°μ μ μ΄ μ£Όμ΄μ§λ€.
μ΄ μ λ€ μ€ μΌλΆλ₯Ό μ νν΄ λ³Όλ‘ λ€κ°ν(Convex Polygon) μ λ§λ€κ³ ,
κ·Έ λ€κ°νμ μ΄λ£¨λ μ μ κ°μλ₯Ό ꡬνλ λ¬Έμ μ΄λ€.
λ¬Έμ μ ν΅μ¬ 쑰건μ λ€μκ³Ό κ°λ€.
μ¦, μ°λ¦¬λ Convex Hull (λ³Όλ‘ κ»μ§) μ ꡬν΄μΌ νλ€.
μ΄ λ¬Έμ λ λνμ μΈ λ³Όλ‘ κ»μ§ μκ³ λ¦¬μ¦μΈ Graham Scan μΌλ‘ ν΄κ²°ν μ μλ€.
Graham Scanμ μ 체 νλ¦μ λ€μκ³Ό κ°λ€.
μ΄ κ³Όμ μ ν΅ν΄ μμ°μ€λ½κ² κ°μ₯ λ°κΉ₯ μ λ€λ§ λ¨κ² λλ€.
κΈ°μ€μ μ λ€μ κΈ°μ€μΌλ‘ μ ννλ€.
μ΄ μ μ λ°λμ λ³Όλ‘ κ»μ§μ ν¬ν¨λλ€.
κ°μ₯ μλμ μμΉν μ μ΄λ―λ‘ λ΄λΆμ ν¬ν¨λ μ μκΈ° λλ¬Έμ΄λ€.
μ½λλ λ€μκ³Ό κ°μ΄ ꡬννμλ€.
for (int i = 0; i < points.size(); i++) {
if (points.get(i).y < root.y) {
root = points.get(i);
} else if (points.get(i).y == root.y) {
if (points.get(i).x < root.x) {
root = points.get(i);
}
}
}
μ΄μ κΈ°μ€μ μ μ€μ¬μΌλ‘ λλ¨Έμ§ μ λ€μ λ°μκ³ λ°©ν₯(CCW) μΌλ‘ μ λ ¬νλ€.
μ¬κΈ°μ μ¬μ©νλ κ²μ΄ ccw() μ°μ°μ΄λ€.
ccw(p1, p2, p3)μ μλ―Έλ λ€μκ³Ό κ°λ€.
μ λ ¬ κΈ°μ€μ λ€μκ³Ό κ°λ€.
μ½λλ λ€μκ³Ό κ°λ€.
Collections.sort(points, (p1, p2) -> {
int result = ccw(root, p1, p2);
if (result > 0) {
return -1;
} else if (result < 0) {
return 1;
} else {
long distance1 = dist(root, p1);
long distance2 = dist(root, p2);
if (distance1 > distance2) {
return 1;
} else {
return -1;
}
}
});
μ¬κΈ°μ ννν κ²½μ° κ°κΉμ΄ μ μ λ¨Όμ λλ μ΄μ λ
μ΄ν μ€ν μ²λ¦¬ κ³Όμ μμ μλμΌλ‘ λ¨Ό μ λ§ λ¨κΈ°κΈ° μν¨μ΄λ€.
μ΄μ μ λ ¬λ μ λ€μ μμλλ‘ λ³΄λ©΄μ λ³Όλ‘ λ€κ°νμ ꡬμ±νλ€.
ν΅μ¬ μμ΄λμ΄λ λ€μκ³Ό κ°λ€.
λ§μ§λ§ λ μ κ³Ό μ μ μ΄
λ°μκ³ λ°©ν₯μ΄ μλ κ²½μ° μ κ±°νλ€.
ꡬν μ½λλ λ€μκ³Ό κ°λ€.
Stack<Point> stack = new Stack<>();
stack.add(root);
for (int i = 1; i < points.size(); i++) {
while (stack.size() > 1 && ccw(stack.get(stack.size() - 2), stack.get(stack.size() - 1), points.get(i)) <= 0) {
stack.pop();
}
stack.add(points.get(i));
}
μ΄ κ³Όμ μ λ°λ³΅νλ©΄ νμ βμ’νμ βλ§ μ μ§λκ² λλ€.
μ¦, λͺ¨λ λ΄κ°μ΄ 180λ μ΄νκ° λλ€.
μ λ ¬ λ¨κ³μμ κ°μ λ°©ν₯(νν)μΌ κ²½μ° κ°κΉμ΄ μ μ λ¨Όμ λ°°μΉνλ€.
μ΄ν μ€ν λ¨κ³μμ
κ²°κ³Όμ μΌλ‘ κ°μ μ§μ μμμλ κ°μ₯ λ¨Ό μ λ§ λ¨κ² λλ€.
κΈ°μ€μ μ κ°μ₯ μλ μ μΌλ‘ μ ννκ³ ,
νμ λ°μκ³ λ°©ν₯λ§ μ μ§νλλ‘ κ΅¬μ±νκΈ° λλ¬Έμ
κ²°κ΅ λ¨λ μ λ€μ κ°μ₯ λ°κΉ₯μ κ°μΈλ μ λ€λΏμ΄λ€.
μ¦, μ΅μ’ stackμ λ¨μ μλ μ λ€μ΄ λ°λ‘ Convex Hullμ΄λ€.
(μ½λ λΆλΆμ κ·Έλλ‘ μ μ§)
// μ 체 μ½λ μλ΅ μμ΄ κ·Έλλ‘ μ½μ
import java.util.*;
import java.io.*;
import java.util.function.Function;
public class Main {
static List<Point> points = new ArrayList<>();
static Point root = new Point(Long.MAX_VALUE, Long.MAX_VALUE);
static class Point {
long y;
long x;
Point(long y, long x) {
this.y = y;
this.x = x;
}
}
static int solve() {
for (int i = 0; i < points.size(); i++) {
if (points.get(i).y < root.y) {
root = points.get(i);
} else if (points.get(i).y == root.y) {
if (points.get(i).x < root.x) {
root = points.get(i);
}
}
} // λ£¨νΈ κ°μ μ ν¨
Collections.sort(points, (p1, p2) -> {
int result = ccw(root, p1, p2);
if (result > 0) {
return -1;
} else if (result < 0) {
return 1;
} else {
long dist1 = distance(root, p1);
long dist2 = distance(root, p2);
if (dist1 > dist2) {
return 1;
}
return -1;
}
});
Stack<Point> stack = new Stack<>();
stack.add(root);
for (int i = 1; i < points.size(); i++) {
while (stack.size() > 1 && ccw(stack.get(stack.size() - 2), stack.get(stack.size() - 1), points.get(i)) <= 0) {
stack.pop();
}
stack.add(points.get(i));
}
return stack.size();
}
static int ccw(Point p1, Point p2, Point p3) {
long angle = ((p1.x * p2.y) + (p2.x * p3.y) + (p3.x * p1.y))
- ((p1.y * p2.x) + (p2.y * p3.x) + (p3.y * p1.x));
return (angle == 0) ? 0
: (angle > 0) ? 1 : -1;
}
static long distance(Point p1, Point p2) {
return ((p2.y - p1.y) * (p2.y - p1.y)) + ((p2.x - p1.x) * (p2.x - p1.x));
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Function<String, Integer> fun = Integer::parseInt;
int N = fun.apply(br.readLine());
for (int i = 0; i < N; i++) {
String[] input = br.readLine().split(" ");
int a = fun.apply(input[0]);
int b = fun.apply(input[1]);
points.add(new Point(a, b));
}
System.out.println(solve());
}
}
μ²μμλ μ§κ΄μ μΌλ‘ μ΄ν΄νκΈ° μ΄λ €μ μ§λ§,
ν΅μ¬μ λ¨ νλμλ€.
"νμ μ’νμ λ§ νμ©νλ€."
μ΄ μλ¦¬λ§ μ΄ν΄νλ©΄
Graham Scanμ κ΅μ₯ν μ°μν μκ³ λ¦¬μ¦μ΄λΌλ κ²μ λλ μ μλ€.
κΈ°ν λ¬Έμ μ€μμλ ꡬ쑰λ₯Ό μ΄ν΄νλ μ¬λ―Έκ° μλ λ¬Έμ μλ€.