https://www.acmicpc.net/problem/1708

주어진 점들의 볼록 껍질의 꼭짓점의 개수를 구하는 문제이다.
볼록 껍질(convex hull)(1), Graham Scan 알고리즘, python
볼록 껍질(convex hull)(1), Graham Scan 알고리즘, python
볼록 껍질이란? 점들의 집합이 있을 때 그 집합에서 임의의 두 점을 이은 선분이 항상 집합에 포함되면 볼록 집합이라 부른다. 왼쪽을 볼록 집합(convex set)이라고 하고 오른쪽을 비볼록집합(non con
aiden0413.tistory.com
볼록 껍질(convex hull)(2), Monotone Chain 알고리즘, python
볼록 껍질(convex hull)(2), Monotone Chain 알고리즘, python
모노톤 체인(Andrew's Monotone Chain) 알고리즘 볼록 껍질(convex hull)(1), Graham Scan 알고리즘, python 볼록 껍질(convex hull)(1), Graham Scan 알고리즘, python볼록 껍질이란? 점들의 집합이 있을 때 그 집합에서 임
aiden0413.tistory.com
그레이엄 스캔 알고리즘을 통해 볼록 껍질의 꼭짓점의 개수를 구해주었다.
import sys, math
input = sys.stdin.readline
n = int(input())
points = [list(map(int, input().split())) for _ in range(n)]
# 외적으로 CCW체크
def ccw(p1,p2,p3):
a,b = p2[0]-p1[0],p2[1]-p1[1]
c,d = p3[0]-p1[0],p3[1]-p1[1]
return a*d-b*c
points.sort(key=lambda p:(p[1],p[0]))
p0 = points[0]
points = points[1:]
# 점을 각도, 거리 순으로 정렬
points.sort(key=lambda p:(math.atan2(p[1]-p0[1],p[0]-p0[0]), (p0[0]-p[0])**2+(p0[1]-p[1])**2))
stack = [p0]
for p in points:
# 스택의 상위 2개와 현재 점이 CW라면 스택에서 꺼냄
while len(stack)>=2 and ccw(stack[-2],stack[-1],p) <= 0:
stack.pop()
stack.append(p)
print(len(stack))

'python' 카테고리의 다른 글
| [백준] 6549 히스토그램에서 가장 큰 직사각형, python (0) | 2025.10.27 |
|---|---|
| [백준] 11668 파이프 청소, python (0) | 2025.10.25 |
| [백준] 20149 선분 교차 3, python (0) | 2025.10.21 |
| [백준] 1707 이분 그래프, python (0) | 2025.10.19 |
| 파이썬 함수 결과값 자동 캐싱(cache, lru_cache), python (0) | 2025.10.15 |