본문 바로가기

반응형

백준

(29)
백준 2109번: 순회강연 파이썬 코드(우선순위 큐) #2109 import sys import heapq heap = [] days = [0 for _ in range(10001)] n = int(sys.stdin.readline()) for _ in range(n): p,d = map(int, sys.stdin.readline().split()) heapq.heappush(heap, [p,d]) heap2 = [] for _ in range(len(heap)): heap2.append(heapq.heappop(heap)) money = 0 while len(heap2) > 0: temp = heap2[-1] del heap2[-1] if days[temp[1]] != 0: while 1: temp[1] = temp[1] - 1 if temp[1] == ..
백준 1715번: 카드 정렬하기 파이썬 코드(우선순위 큐) #1715 import heapq import sys n = int(sys.stdin.readline()) heap = [] carculate = [] for _ in range(n): card = int(sys.stdin.readline()) heapq.heappush(heap, card) while len(heap) > 1: temp1 = heapq.heappop(heap) temp2 = heapq.heappop(heap) carculate.append(temp1 + temp2) heapq.heappush(heap,temp1 + temp2) print(sum(carculate)) 설명 숫자끼리 더하면서 생기는 연산을 모았다가 더하면 되는 문제이다. 다만 적은 수끼리 더하는 것이 연산이 적기 때문에, ..
백준 1374번: 강의실 파이썬 코드(우선순위 큐) import heapq import sys n = int(sys.stdin.readline()) heap = [] q = [] count = 0 for _ in range(n): num, start, end = map(int,sys.stdin.readline().split()) heapq.heappush(heap, [start,end,num]) start, end, num = heapq.heappop(heap) heapq.heappush(q, end) while heap: start, end, num = heapq.heappop(heap) if q[0]
백준 1010번: 다리 놓기 파이썬 코드 import math t = int(input()) for i in range(t): n, m = map(int, input().split()) if n < m: temp = m m = n n = temp print(int(math.factorial(n)/(math.factorial(n-m) * math.factorial(m)))) 다른 언어였다면 팩토리얼을 따로 함수로 만들어야하겠지만, 난 그냥 math에 있는 함수 가져다가 풀었다. 푼지 오래된 문제라 푼 과정은 잘 기억나지 않는다.
백준 1012번: 유기농 배추 파이썬 코드(dfs) import sys sys.setrecursionlimit(100000) t = int(sys.stdin.readline()) xx = [-1,1,0,0] yy = [0,0,-1,1] def dfs(a, b): graph[a][b] = -1 for i in range(4): if 0

반응형