목차

Convex Hull

ps
링크acmicpc.net/…
출처BOJ
문제 번호4181
문제명Convex Hull
레벨플래티넘 5
분류

기하학

시간복잡도O(t*nlogn)
인풋사이즈n<=100,000
사용한 언어Python 3.11
제출기록52768KB / 296ms
최고기록296ms
해결날짜2023/04/26

풀이

코드

"""Solution code for "BOJ 4181. Convex Hull".

- Problem link: https://www.acmicpc.net/problem/4181
- Solution link: http://www.teferi.net/ps/problems/boj/4181

Tags: [geometry]
"""

import bisect
import math
import sys
from teflib import geometry


def main():
    n = int(sys.stdin.readline())
    points = []
    for _ in range(n):
        x, y, c = sys.stdin.readline().split()
        if c == 'Y':
            points.append([int(x), int(y)])

    pole = min(points)
    get_angle = geometry.polar_angle_key(pole)
    points.sort(key=get_angle)

    first_angle = get_angle(points[1])
    pos = bisect.bisect_right(points, first_angle, key=get_angle)
    points[1:pos] = sorted(points[1:pos], key=lambda p: math.dist(p, pole))

    last_angle = get_angle(points[-1])
    pos = bisect.bisect_left(points, last_angle, key=get_angle)
    points[pos:] = sorted(points[pos:], key=lambda p: -math.dist(p, pole))

    print(len(points))
    for p in points:
        print(*p)


if __name__ == '__main__':
    main()