2467: 용액

ewillwin·2023년 10월 31일
0

Problem Solving (BOJ)

목록 보기
228/230

문제 링크

2467: 용액


구현 방식

  • 이분 탐색으로 풀어주었다
    • l < r을 만족할 때까지, value = A[l] + A[r]이 음수라면 l+=1, 양수라면 r-=1
    • abs(value)가 near_value(지금까지 가장 0과 가까운 value) 보다 작다면 x, y, near_value 갱신

코드

import sys
from collections import deque

N = int(sys.stdin.readline().strip())
A = list(map(int, sys.stdin.readline().strip().split()))

near_value = int(10e9)

l = 0; r = N-1; x = 0; y = 0
while l < r:
    value = A[l] + A[r]

    if abs(value) <= near_value:
        x = A[l]; y = A[r]
        near_value = abs(value)
    
    if value <= 0: l+=1
    else: r-=1
print(x, y)
profile
Software Engineer @ LG Electronics

0개의 댓글