practivceAlgorithm/백준
[백준][Python] 2474 세 용액
findTheValue
2021. 10. 7. 17:32
투포인터를 각 출발선마다 돌리는 로직입니다.
정렬후 앞에서 하나씩 고정값으로 픽스시켜두고 두가지 포인터로 합의 범위를 0으로 수렴시켜나가는 과정입니다.
import sys
input = sys.stdin.readline
n = int(input())
liquid = sorted(list(map(int, input().split())))
close_zero = float('inf')
selected = [0]*3
for start in range(n - 2):
left, right = start + 1, n - 1
while left < right:
sum_value = abs(liquid[start] + liquid[left] + liquid[right])
if sum_value < close_zero:
selected = [liquid[start], liquid[left], liquid[right]]
close_zero = sum_value
if sum_value < 0:
left += 1
elif sum_value > 0:
right -= 1
else:
print(*selected)
exit()
print(*selected)