[BOJ 10903] Wall construction
문제
https://www.acmicpc.net/problem/10903
풀이
맹독 방벽와 거의 동일한 문제이다.
코드
import sys
from math import pi
def tangent(x, y):
if x == a:
return 1, y
if x > a:
return 0, (y - b) / (x - a), x
if x < a:
return 2, (y - b) / (x - a), x
def isline(x, y, z):
return (y[1] - x[1]) * (z[0] - y[0]) == (z[1] - y[1]) * (y[0] - x[0])
n, r = map(int, sys.stdin.readline().split())
coord = [tuple(map(int, sys.stdin.readline().split())) for _ in range(n)]
coord.sort(key=lambda x: (x[1], x[0]))
a, b = coord.pop(0)
coord.sort(key=lambda x: tangent(x[0], x[1]))
stack = [(a, b), coord[0]]
idx = 0
while idx < n - 2:
idx += 1
w1, w2 = coord[idx]
while len(stack) >= 2:
u1, u2 = stack[-2]
v1, v2 = stack[-1]
if (v1 - u1) * (w2 - v2) <= (v2 - u2) * (w1 - v1):
stack.pop()
else:
break
stack.append((w1, w2))
if (len(stack) >= 3) and isline(stack[-3], stack[-2], stack[-1]):
stack.pop(-2)
if (len(stack) >= 3) and isline(stack[-2], stack[-1], stack[0]):
stack.pop()
res = 2 * pi * r
for i in range(-1, len(stack) - 1):
res += ((stack[i][0] - stack[i + 1][0]) ** 2 + (stack[i][1] - stack[i + 1][1]) ** 2) ** 0.5
print(res)
Leave a comment