Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys
input = sys.stdin.readline

N = int(input()) # 전체 용액의 수
liquid = sorted(map(int, input().split()))

left = 0
right = N - 1

# 초기값 설정
answer = abs(liquid[left] + liquid[right])
answer_liquid = [liquid[left], liquid[right]]

while left < right:
temp = liquid[left] + liquid[right]
# 합이 0에 더 가까우면 정답 갱신
if abs(temp) < answer:
answer = abs(temp)
answer_liquid = [liquid[left], liquid[right]]
# 포인터 이동
if temp < 0:
left += 1
else:
right -= 1

print(answer_liquid[0], answer_liquid[1])
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sys
input = sys.stdin.readline

N = int(input()) # 전체 용액의 수
liquid = sorted(map(int, input().split()))

left = 0
right = N-1
mid = (left + right) // 2
answer = abs(liquid[left] + liquid[mid] + liquid[right])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 세 용액 총합의 최소값을 INF로 설정했는데 민정님은 첫번째 경우의 합으로 설정하셨네요. 초기화는 필수가 아닌 이상 저는 설정하지 않고 그냥 사용했던 것 같습니다.
그래도 민정님처럼 중간값, 세 용액 조합 리스트 같이 아래에서 새로 대입하는 변수지만 위에서 초기화 하고 사용하는 것이 가독성과 변수 활용에 용이해 보이네요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다!

answer_liquid = [liquid[left], liquid[mid], liquid[right]]

for i in range(N-2):
left = i + 1
right = N - 1

while left < right:
temp = liquid[i] + liquid[left] + liquid[right]
if abs(temp) < answer:
answer = abs(temp)
answer_liquid = [liquid[i], liquid[left], liquid[right]]
if temp < 0:
left += 1
else:
right -= 1
Comment on lines +13 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 i를 고정한 뒤에 left는 0부터 right는 N-1부터 시작하게 했는데 민정님 코드 보니까 left를 i+1로 해도 되겠네요.
민정님이 리뷰 남겨주신 아래 부분도 left가 0부터 시작하니 i가 left나 right와 같아질 수도 있어서 추가한 거거든요.

  if left == i:
            left = i + 1
            continue
        elif right == i:
            right = i - 1
            continue

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇군요!! 윤상님처럼 접근할 수 있다는 점 참고해야겠네요!!


print(*answer_liquid)
46 changes: 46 additions & 0 deletions minjeong/UnionFind/2025-04-26-[백준]-#5107-마니또.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import sys

input = sys.stdin.readline


# Find 연산(같은 집합에 속하는지 확인하기 위한 함수)
def find(a):
if a != parent[a]:
parent[a] = find(parent[a]) # 경로 압축
return parent[a]


# Union 연산(두 집합을 합치기 위한 함수)
def union(a, b):
p_a = find(a)
p_b = find(b)

if p_a > p_b: # 값이 더 작은 쪽을 부모로 설정
parent[p_a] = p_b
else:
parent[p_b] = p_a


tc_num = 0 # 테스트케이스 개수
while True:
N = int(input())
parent = [i for i in range(N + 1)] # 초기: 각 원소가 자기 자신을 부모로 가짐
manito = {}
tc_num += 1 # 테스트케이스 업데이트

if N == 0: # 입력 종료
break

for _ in range(N):
from_p, to_p = input().split()
# manito에 번호 부여
if from_p not in manito:
manito[from_p] = len(manito) + 1
if to_p not in manito:
manito[to_p] = len(manito) + 1
# 합집합 연산
union(parent[manito[from_p]], parent[manito[to_p]])

parent = set(parent)

print(tc_num, len(parent) - 1) # 0이 포함되어 있으므로 1 빼주어야 함