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
44 changes: 44 additions & 0 deletions YoonYn9915/2025-06-07-[백준]-#1715-카드 정렬하기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''
요구사항
1. 매우 많은 숫자 카드 묶음이 책상 위에 놓여 있다. 이들을 두 묶음씩 골라 서로 합쳐나갈 때, 최소한 몇 번의 비교가 필요한지를 계산

1. 아이디어
- A개, B개 두 묶음의 카드를 비교하는데 A+B번 비교해야 함.
- 비교한 횟수를 누적시켜 최소값을 찾아야 하므로 비교횟수가 가장 작은 것들부터 시작해야 함.
- A, B, C 3개의 카드 묶음이 있고 A < B < C라면, (A+B) + ((A+B) + C)가 최소값이다.

2. 시간복잡도
- (1 ≤ N ≤ 100,000)
- O(N + (N-1) * logN), 대략 O(NlogN)으로 시간복잡도 만족.

3. 구현
3.1 입력받기
3.2 카드 묶음 정렬(우선순위 큐에 저장)
3.3 가장 카드가 적은 묶음 2개를 뽑아 더한 값을 정답변수에 누적시키고 다시 우선순위큐에 저장
3.4 우선순위 큐가 하나 남을때까지 반복
3.5 정답 출력
'''

import sys
import heapq

inp = sys.stdin.readline

N = int(inp().strip())
arr = []
answer = 0

for _ in range(N):
arr.append(int(inp().strip()))

# 매번 최소 카드 묶음 2개를 뽑기 위해 heap 사용
heapq.heapify(arr)

while len(arr) > 1:
min1 = heapq.heappop(arr)
min2 = heapq.heappop(arr)

answer += min1 + min2
heapq.heappush(arr, min1 + min2)

print(answer)
27 changes: 27 additions & 0 deletions YoonYn9915/Graph/2025-06-07-[백준]-#1388-바닥 장식.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

def dfs(x, y):
if graph[x][y] == '-':
graph[x][y] = 1
for _y in [1, -1]:
Y = y + _y
if (Y > 0 and Y < m) and graph[x][Y] == '-':
dfs(x, Y)
if graph[x][y] == '|':
graph[x][y] = 1
for _x in [1, -1]:
X = x + _x
if (X > 0 and X < n) and graph[X][y] == '|':
dfs(X, y)


n, m = map(int, input().split())
graph = []
for _ in range(n):
graph.append(list(input()))

count = 0
for i in range(n):
for j in range(m):
if graph[i][j] == '-' or graph[i][j] == '|':
dfs(i, j)
count += 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'''
요구사항
1. A만큼의 에너지를 가진 슬라임과 B만큼의 에너지를 가진 슬라임을 합성하려면 A × B 만큼의 에너지 필요.
2. N마리의 슬라임들을 적절히 합성해서 1마리의 슬라임으로 만들때, 필요한 에너지 값을 모두 곱한 값을 최소로 만든다.
3. 슬라임을 모두 합성했을 때 청구될 비용의 최솟값을 1, 000, 000, 007로 나눈 나머지를 출력한다. 에너지가 전혀 필요하지 않은 경우엔 1 을 출력한다.

1. 아이디어
곱의 누적곱이 최소가 되게 하려면 곱셈의 결과가 최소가 나오게 해야 함. 즉 큰 수를 가장 적게 곱하고
되도록 작은수 X 작은수 형태로 풀이. 따라서 각 경우마다 가장 작은 두 수를 그리디하게 뽑아서 곱하고 누적곱해주면 된다.

2. 시간복잡도
슬라임 수 N (1 ≤ N ≤ 60), i번째 슬라임의 에너지 Ci (2 ≤ Ci ≤ 2 × 1018)일때,
모든 테스트 케이스에 대한 N 의 총합이 1, 000, 000을 넘지 않음으로 테스트 케이스의 수는 최대 500,000.
즉 O(test_case * (N-1)) == O(500,000 * 59)로 만족.
Comment on lines +11 to +14
Copy link
Collaborator

Choose a reason for hiding this comment

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

시간복잡도까지 구체적으로 적어놓는 점이 배울 점인 것 같네요!


3. 구현
3-1. 입력받기
3-2. N-1번 가장 작은 두 수를 뽑는다.
3-3. 두 수를 곱하고 정답변수에 누적시킨다.
3-4. 곱한 두 슬라임을 제거하고, 새 슬라임을 추가한다.
3-5. 정답 출력

'''


import heapq
import sys

inp = sys.stdin.readline
MOD = 1000000007

test_case = int(inp())

for _ in range(test_case):
N = int(inp())
arr = list(map(int, inp().split()))

if N == 1:
print(1)
continue

hq = []
for num in arr:
heapq.heappush(hq, num)
Comment on lines +42 to +44
Copy link
Collaborator

Choose a reason for hiding this comment

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

저는 이 부분을
heapq.heapify(arr) 이런 식으로 작성했습니다!

Copy link
Member Author

Choose a reason for hiding this comment

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

이 문제 풀 당시에는 heapify가 생각이 안났어요 ㅎㅎ


result = 1
while len(hq) > 1:
a = heapq.heappop(hq)
b = heapq.heappop(hq)
energy = a * b
result = (result * energy) % MOD
Copy link
Collaborator

Choose a reason for hiding this comment

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

어차피 heap에 저장되어야 하니까 저장할 떄 MOD로 나누는 게 더 좋겠네요! 저는 마지막에 출력할 때만 나누었거든요..

heapq.heappush(hq, energy)

print(result)