본문 바로가기

Algorithm245

[백준] 1967번 - 트리의 지름 (파이썬) # 트리의 지름 from collections import deque import sys input = sys.stdin.readline V = int(input()) arr = [[] for _ in range(V+1)] for _ in range(V): temp = list(map(int, input().split())) v = temp[0] for i in range(1, len(temp)-2, 2): arr[v].append((temp[i], temp[i+1])) far_node = 0 def find(a, c): global far_node q = deque() q.append((a, c)) visited = [False] * (V+1) visited[a] = True result = 0 whi.. 2022. 1. 27.
[백준] 14889번 - 스타트와 링크 (파이썬) # 스타트와 링크 import sys MAX = sys.maxsize N = int(input()) arr = [] for _ in range(N): arr.append(list(map(int, input().split()))) people = [i for i in range(N)] start_team = [] result = MAX def chooseMember(num, k): global result if num == N//2: link_team = [] link_team = list(set(people) - set(start_team)) result = min(result, abs(getScore(link_team) - getScore(start_team))) return for i in range.. 2022. 1. 27.
[백준] 14501번 - 퇴사 (파이썬) # 퇴사 N = int(input()) arr = [] for _ in range(N): day, pay = map(int, input().split()) arr.append((day, pay)) dp = [0] * (N+1) for i in range(N-1, -1, -1): day = arr[i][0] pay = arr[i][1] if day + i 2022. 1. 27.
[백준] 15685번 - 드래곤 커브 (파이썬) # 드래곤 커브 import sys input = sys.stdin.readline T = int(input()) arr = [[False] * 101 for _ in range(101)] dx = [0, -1, 0, 1] dy = [1, 0, -1, 0] q = [] for _ in range(T): y, x, d, g = map(int, input().split()) q.append((x, y, d, g)) def dragon_curve(): for x, y, d, g in q: arr[x][y] = True dir = [d] for _ in range(g): for d in range(len(dir)-1, -1, -1): temp = (dir[d] + 1) % 4 dir.append(temp) fo.. 2022. 1. 26.
[백준] 14890번 - 경사로 (파이썬) # 경사로 import sys input = sys.stdin.readline N, L = map(int, input().split()) arr = [] for _ in range(N): arr.append(list(map(int, input().split()))) result = 0 def check(line): global result isOK = True isSlope = [False] * N for i in range(N-1): if line[i] == line[i+1]: continue elif abs(line[i] - line[i+1]) >= 2: isOK = False break elif abs(line[i] - line[i+1]) == 1: if slope_check(line, i, isS.. 2022. 1. 26.
[백준] 1655번 - 가운데를 말해요 (파이썬) # 가운데를 말해요 import sys import heapq input = sys.stdin.readline N = int(input()) left = [] right = [] for _ in range(N): if len(left) == len(right): heapq.heappush(left, -int(input())) else: heapq.heappush(right, int(input())) if len(left) != 0 and len(right) != 0 and -left[0] > right[0]: add_left = heapq.heappop(right) add_right = heapq.heappop(left) heapq.heappush(left, -add_left) heapq.heappush.. 2022. 1. 26.
[백준] 2573번 - 빙산 (파이썬) # 빙산 from collections import deque import sys input = sys.stdin.readline N, M = map(int, input().split()) arr = [] ice = [] melt = [] for i in range(N): arr.append(list(map(int, input().split()))) for j in range(M): if arr[i][j] != 0: ice.append((i, j)) dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] def melt_check(): for x, y in ice: if arr[x][y] != 0: zero = 0 for k in range(4): nx = x + dx[k] ny = y + .. 2022. 1. 25.