Skip to content

Commit 9a558c6

Browse files
committed
[BOJ]#2473. 세 용액/골드3/힌트
https://www.acmicpc.net/problem/2473
1 parent 467a01c commit 9a558c6

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

Hongjoo/백준/세용액.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
https://www.acmicpc.net/problem/2473
3+
# 두용액 -> 3용액
4+
# 포인트
5+
#flow
6+
x<= y<= z 일떄
7+
(1) (x,y) 의 모든 조합
8+
(2)z 는 y+1 ~ N 번째 중 x+y+z -> 0 인 숫자 구하기
9+
#ME
10+
(1) Start idx = 0 , end _iex =-1
11+
(2) middle : for문으로 start+1 : end 내 값 중 -(start+end)과 가장 가까운 값 ?
12+
(3) start + end + miidle 합과 기존 최소값 비교하기
13+
14+
4
15+
1 2 3 4
16+
17+
4
18+
-1 -2 -3 -4
19+
20+
4
21+
-2 -1 1 2
22+
23+
# 0 가능
24+
6
25+
-10 0 2 3 4 8
26+
=> -10 2 8
27+
28+
"""
29+
30+
31+
import sys
32+
33+
INF = 1e12
34+
N = int(sys.stdin.readline())
35+
arr = sorted(list(map(int, sys.stdin.readline().split())))
36+
#1. 오름차순 정렬 과 포인트 초기화
37+
38+
total_min = INF
39+
answer = []
40+
# print(f"arr {arr}")
41+
# 2. 투 포인터
42+
# x < y<z 일때 - X 는 fix , y,z는 투 포인터
43+
for i in range(N-2):
44+
x = arr[i]
45+
yp = i+1
46+
zp =N-1
47+
while yp < zp :
48+
xyz_sum =x + arr[yp] + arr[zp]
49+
#결과값 업데이트
50+
if abs(xyz_sum) < total_min : # 업데이트
51+
answer = [x,arr[yp],arr[zp]]
52+
total_min = abs(xyz_sum)
53+
# 포인터 이동
54+
if xyz_sum <0 :
55+
yp+=1
56+
elif xyz_sum >0 :
57+
zp-=1
58+
else : # xyz_sum == 0
59+
print(" ".join(map(str,answer)))
60+
sys.exit()
61+
62+
63+
64+
str_answer = " ".join(map(str,answer))
65+
print(str_answer)

0 commit comments

Comments
 (0)