목록문제/백준_파이썬 (155)
sm 기술 블로그
import sys sys.setrecursionlimit(10**8) input = sys.stdin.readline M, N = map(int, input().split()) travleMap = [list(map(int, input().split())) for _ in range(M)] dp = [[-1] * N for _ in range(M)] def dfs(x, y): # 도착지점 if x == M-1 and y == N-1: return 1 # -1이면 방문하지 않았다는 것임. if dp[x][y] == -1: # 목적지까지 갈 수 없는 경우는 0으로 반환 dp[x][y] = 0 # dx, dy 는 현재 기준 상하좌우 for dx, dy in (1, 0), (-1, 0), (0, -1), (0..
본 코드는 pypy3로 제출해야 합니다. import sys input = sys.stdin.readline N = int(input()) rc = [list(map(int, input().split())) for _ in range(N)] DP = [[0]*N for _ in range(N)] for i in range(1, N): for x in range(N-i): y = i + x DP[x][y] = 2 ** 32 for k in range(x, y): DP[x][y] = min(DP[x][y], DP[x][k] + DP[k+1][y] + rc[x][0] * rc[k][1] * rc[y][1]) print(DP[0][-1]) 문제요약 행렬의 곱셈 연산이 제일 적게 이행하는 최솟값을 구하라. 설명 ..
import sys input = sys.stdin.readline T = int(input()) result = [] for _ in range(T): K = int(input()) page = [0] + list(map(int, input().split())) sumPage = [0 for _ in range(K+1)] for i in range(1, K+1): sumPage[i] = sumPage[i-1] + page[i] DP = [[0]*(K+1) for _ in range(K+1)] for i in range(2, K+1): for j in range(1, K+2-i): DP[j][j+i-1] = min([DP[j][j+k] + DP[j+k+1][j+i-1] for k in range(i-1)..
import heapq import sys input = sys.stdin.readline N = int(input()) leftHeap = [] rightHeap = [] result = [] for i in range(N): x = int(input()) if len(leftHeap) == len(rightHeap): heapq.heappush(leftHeap, -x) else: heapq.heappush(rightHeap, x) if rightHeap and rightHeap[0] < -leftHeap[0]: leftValue = heapq.heappop(leftHeap) rigthValue = heapq.heappop(rightHeap) heapq.heappush(leftHeap, -rigthVa..