Upload files to "백준"

This commit is contained in:
2026-04-11 11:47:49 +09:00
parent 965ff9e4b1
commit e9e87657ab
5 changed files with 157 additions and 0 deletions

36
백준/1516.py Normal file
View File

@@ -0,0 +1,36 @@
import sys
import math
from collections import deque
input = sys.stdin.readline
N = int(input())
time = [0] * (N+1)
build_time = [0] * (N+1)
a = [[] for _ in range(N+1)]
indegree = [0] * (N+1)
for i in range(1, N+1):
temp = list(map(int, input().split()))
build_time[i] = temp[0]
time[i] = temp[0]
for k in temp[1:-1]:
a[k].append(i)
indegree[i] += 1
queue = deque()
for i in range(1, N+1):
if(indegree[i]==0):
queue.append(i)
while queue:
curr = queue.popleft()
for next in a[curr]:
indegree[next] -= 1
build_time[next] = max(build_time[next], build_time[curr] + time[next])
if indegree[next] == 0:
queue.append(next)
for i in range(1, N+1):
print(build_time[i])

44
백준/1517.Py Normal file
View File

@@ -0,0 +1,44 @@
import sys
N = int(sys.stdin.readline().rstrip())
arr = list(map(int, input().split(' ')))
arr.insert(0, 0)
tarr = [0] * (N+1)
num=0
def ms(s, e):
global num
if(e-s<1):
return
m = int(s + (e-s)/2)
ms(s, m)
ms(m+1, e)
for i in range(s, e+1):
tarr[i] = arr[i]
k=s
index1 = s
index2 = m+1
while index1<=m and index2 <=e :
if tarr[index1] > tarr[index2]:
arr[k]=tarr[index2]
num = num + m
k += 1
index2+=1
else:
arr[k]=tarr[index1]
k += 1
index1 += 1
while index1<=m:
arr[k]=tarr[index1]
k += 1
index1 += 1
while index2<=e:
arr[k]=tarr[index2]
k+=1
index2+=1
ms(1, N)
print(num)

25
백준/1654.py Normal file
View File

@@ -0,0 +1,25 @@
import sys
input = sys.stdin.readline
k, n = map(int, input().split())
arr = []
for _ in range(k):
arr.append(int(input()))
start = 1
end = max(arr)
result = 0
while start <= end:
mid = (start + end) // 2
count = 0
for i in range(k):
count+=arr[i]//mid
if count >= n:
result = mid
start = mid + 1
else:
end = mid - 1
print(result)

23
백준/1655.py Normal file
View File

@@ -0,0 +1,23 @@
import sys
from heapq import heappop, heappush
input = sys.stdin.readline
n = int(input())
min_heap = []
max_heap = []
for i in range(n):
k = int(input())
if len(min_heap) == len(max_heap):
heappush(max_heap, -k)
else:
heappush(min_heap, k)
if min_heap and -max_heap[0] > min_heap[0] :
temp_max = heappop(max_heap)
temp_min = heappop(min_heap)
heappush(max_heap, -temp_min)
heappush(min_heap, -temp_max)
print(-max_heap[0])

29
백준/1697.py Normal file
View File

@@ -0,0 +1,29 @@
from collections import deque
n, K = map(int, input().split())
max_val = 100001 # 문제 범위: 0 ≤ 위치 ≤ 100000
# 각 위치에 도달하는 최소 시간 (방문하지 않은 경우 -1)
dist = [-1] * max_val
# 각 위치에 도달하는 방법의 수
ways = [0] * max_val
q = deque([n])
dist[n] = 0
ways[n] = 1
while q:
cur = q.popleft()
for next in (cur - 1, cur + 1, cur * 2):
if 0 <= next < max_val:
# 아직 방문하지 않은 경우
if dist[next] == -1:
dist[next] = dist[cur] + 1
ways[next] = ways[cur]
q.append(next)
# 동일한 최소 시간으로 도달하는 경우
elif dist[next] == dist[cur] + 1:
ways[next] += ways[cur]
print(dist[K])
print(ways[K])