Skip to content

Commit b372219

Browse files
committed
[BOJ] 세 용액 / 골드 3 / 80분 힌트사용
https://www.acmicpc.net/problem/2473
1 parent 73b3aca commit b372219

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sys
2+
3+
inp = sys.stdin.readline
4+
5+
N = int(inp())
6+
arr = list(map(int, inp().split()))
7+
arr.sort()
8+
9+
min_value = int(1e10)
10+
answer = [-1, -1, -1]
11+
12+
# 각 용액을 순회하며
13+
for i in range(N):
14+
left = i + 1
15+
right = N - 1
16+
17+
# i번 용액을 넣었을 때 값이 가장 0에 가까운 두 값 찾기
18+
while left < right:
19+
if left == i:
20+
left = i + 1
21+
continue
22+
elif right == i:
23+
right = i - 1
24+
continue
25+
26+
total = arr[i] + arr[left] + arr[right]
27+
28+
# 세 용액의 합이 0에 가장 가까우면 업데이트
29+
if abs(total) < min_value:
30+
min_value = abs(total)
31+
answer = [arr[i], arr[left], arr[right]]
32+
33+
if total == 0:
34+
break
35+
elif total < 0:
36+
left += 1
37+
else:
38+
right -= 1
39+
40+
answer.sort()
41+
42+
for ans in answer:
43+
print(ans, end=" ")

0 commit comments

Comments
 (0)