From 11f50da3aaf49056e22271d0e77301d6bbace8ca Mon Sep 17 00:00:00 2001 From: ddingmin Date: Fri, 3 Nov 2023 23:55:04 +0900 Subject: [PATCH 01/50] [BOJ] 25319: Twitch Plays VIIIbit Explorer --- ddingmin/baekjoon/25319.py | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 ddingmin/baekjoon/25319.py diff --git a/ddingmin/baekjoon/25319.py b/ddingmin/baekjoon/25319.py new file mode 100644 index 0000000..671a4e9 --- /dev/null +++ b/ddingmin/baekjoon/25319.py @@ -0,0 +1,89 @@ +import sys + +sys.setrecursionlimit(10 ** 5) +input = sys.stdin.readline + +# input +n, m, r = map(int, input().split()) +alphabets = {} +arr = [list(input().strip()) for _ in range(n)] + +# 이동하기 위해 알파벳을 키로 갖는 좌표들을 저장한다. +for i in range(n): + for j in range(m): + if arr[i][j] not in alphabets: + alphabets[arr[i][j]] = [[i, j]] + else: + alphabets[arr[i][j]].append([i, j]) + +target_id = input().strip() + +# 목표 id에 대한 갯수를 저장한다. +target_id_count = {} + +for i in target_id: + if i not in target_id_count: + target_id_count[i] = 1 + else: + target_id_count[i] += 1 + + +# func +def calculate_can_levelUp(): + # 레벨업 할 수 있는 최대치를 계산한다. + result = float('inf') + + for k in target_id_count: + if k not in alphabets: + return 0 + else: + result = min(len(alphabets[k]) // target_id_count[k], result) + return result + + +def get_cmd(i, j, x, y): + result = "" + if i > x: + result += "U" * (i - x) + else: + result += "D" * (x - i) + + if j > y: + result += "L" * (j - y) + else: + result += "R" * (y - j) + return result + "P" + + +def backtracking(i, j, need_idx, words): + global ans + # 갱신되는 경우 + if need_idx and need_idx % len(target_id) == 0: + ans = [need_idx // len(target_id), words + get_cmd(i, j, n - 1, m - 1)[:-1]] # 도착 지점까지 이동할 경우 P 명령어는 제거 + + if ans[0] == can_levelUp: # 최대 레벨업에 도달하면 출력 후 종료 + print(ans[0], len(ans[1])) + print(ans[1]) + exit() + + # 목표한 다음 위치의 좌표로 이동 + for x, y in alphabets[target_id[need_idx % len(target_id)]]: + if ate[x][y]: + continue + ate[x][y] = 1 + backtracking(x, y, need_idx + 1, words + get_cmd(i, j, x, y)) # 이동 지점까지 명령어를 더 붙여준다. + ate[x][y] = 0 + + +# solve +can_levelUp = calculate_can_levelUp() +ans = [0, ""] + +if can_levelUp: + ate = [[0] * m for _ in range(n)] + backtracking(0, 0, 0, "") +else: # 레벨업 할 수 없는 경우 + ans = [0, get_cmd(0, 0, n - 1, m - 1)[:-1]] # 시작 지점 부터 오른쪽 가장 하단까지 이동 + +print(ans[0], len(ans[1])) +print(ans[1]) From 984596c18d2fd01520c250c6f1708156a33288ef Mon Sep 17 00:00:00 2001 From: ddingmin Date: Fri, 3 Nov 2023 01:29:24 +0900 Subject: [PATCH 02/50] =?UTF-8?q?[BOJ]=2014497:=20=EC=A3=BC=EB=82=9C?= =?UTF-8?q?=EC=9D=98=20=EB=82=9C(=E9=9B=A3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/14497.py | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 ddingmin/baekjoon/14497.py diff --git a/ddingmin/baekjoon/14497.py b/ddingmin/baekjoon/14497.py new file mode 100644 index 0000000..024e8f7 --- /dev/null +++ b/ddingmin/baekjoon/14497.py @@ -0,0 +1,56 @@ +import sys +from collections import deque + +input = sys.stdin.readline + +dx, dy = [1, -1, 0, 0], [0, 0, 1, -1] + +# input +n, m = map(int, input().split()) +r, c, rr, cc = map(int, input().split()) +r, c, rr, cc = r - 1, c - 1, rr - 1, cc - 1 + +arr = [list(input().strip()) for _ in range(n)] + +visited = [[0] * m for _ in range(n)] + + +def jump(): + q = deque() + visit = [[0] * m for _ in range(n)] + visit[r][c] = 1 + q.append([r, c]) + + delete = [] + + while q: + i, j = q.popleft() + for k in range(4): + x, y = i + dx[k], j + dy[k] + if not (0 <= x < n and 0 <= y < m): continue + if visit[x][y]: continue + + if visited[x][y] == 1: + visit[x][y] = 1 + q.append([x, y]) + elif visited[x][y] == 0 and arr[x][y] == '0': + visited[x][y] = 1 + visit[x][y] = 1 + q.append([x, y]) + elif visited[x][y] == 0 and arr[x][y] != '0': + delete.append([x, y]) + + for x, y in delete: + visited[x][y] = 1 + arr[x][y] = '0' + + +ans = 0 +arr[r][c] = '0' +visited[r][c] = 1 + +while arr[rr][cc] == '#': + ans += 1 + jump() + +print(ans) From 86264f53d163d6c43686f68d255b5946e2ec7b0a Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sun, 5 Nov 2023 19:42:53 +0900 Subject: [PATCH 03/50] =?UTF-8?q?[BOJ]=2025710:=20=EC=A0=90=EC=88=98=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/25710.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 ddingmin/baekjoon/25710.py diff --git a/ddingmin/baekjoon/25710.py b/ddingmin/baekjoon/25710.py new file mode 100644 index 0000000..c3ef00a --- /dev/null +++ b/ddingmin/baekjoon/25710.py @@ -0,0 +1,38 @@ +import sys + +input = sys.stdin.readline + +n = int(input()) +arr = list(map(int, input().split())) + + +def get_dict_numbers(arr): + result = {} + for num in arr: + if num not in result: + result[num] = 0 + result[num] += 1 + return result + + +def get_scores(num1, num2): + result = 0 + for num in str(num1 * num2): + result += int(num) + return result + + +def solve(n, arr): + answer = 0 + + num_counter = get_dict_numbers(arr) + for num1 in num_counter.keys(): + for num2 in num_counter.keys(): + # 두 수가 같은 경우 2번 이상 나오지 않는 경우의 예외 처리한다. + if num1 == num2 and num_counter[num1] == 1: + continue + answer = max(answer, get_scores(num1, num2)) + return answer + + +print(solve(n, arr)) From 15c708f1415945f4df8b2fab7fabd14630d8e6c0 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 7 Nov 2023 00:19:21 +0900 Subject: [PATCH 04/50] =?UTF-8?q?[BOJ]=2019845:=20=EB=84=B4=EB=AA=A8?= =?UTF-8?q?=EB=84=B4=EB=AA=A8=202020?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/19845.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 ddingmin/baekjoon/19845.py diff --git a/ddingmin/baekjoon/19845.py b/ddingmin/baekjoon/19845.py new file mode 100644 index 0000000..7395f84 --- /dev/null +++ b/ddingmin/baekjoon/19845.py @@ -0,0 +1,23 @@ +import sys + +input = sys.stdin.readline + +n, q = map(int, input().split()) +arr = list(map(int, input().split())) + + +def find_x(arr, value): + left, right = 0, len(arr) + while left + 1 < right: + mid = (left + right) // 2 + if arr[mid] < value: + right = mid + else: + left = mid + return right + + +for _ in range(q): + x, y = map(int, input().split()) + answer = max(0, arr[y - 1] - x + 1) + max(0, find_x(arr, x) - y) + print(answer) From 634efeb95798f8c8c0124fcf4677509cac70c272 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Thu, 9 Nov 2023 16:09:33 +0900 Subject: [PATCH 05/50] =?UTF-8?q?[BOJ]=2018188:=20=EB=8B=A4=EC=98=A4?= =?UTF-8?q?=EC=9D=98=20=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/18188.py | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 ddingmin/baekjoon/18188.py diff --git a/ddingmin/baekjoon/18188.py b/ddingmin/baekjoon/18188.py new file mode 100644 index 0000000..b7c0e42 --- /dev/null +++ b/ddingmin/baekjoon/18188.py @@ -0,0 +1,55 @@ +import sys +from collections import deque + + +def main(): + input = sys.stdin.readline + + dir = {'W': [-1, 0], 'S': [1, 0], 'A': [0, -1], 'D': [0, 1]} + + h, w = map(int, input().split()) + arr = [list(input().strip()) for _ in range(h)] + + # 다오 제거 + for i in range(h): + for j in range(w): + if arr[i][j] == 'D': + arr[i][j] = '.' + sx, sy = i, j + + n = int(input()) + + move = [list(map(str, input().split())) for _ in range(n)] + + def solve(sx, sy): + q = deque() + q.append([sx, sy, ""]) + visit = [[0] * w for _ in range(h)] + visit[sx][sy] = 1 + + while q: + i, j, cmd = q.popleft() + if arr[i][j] == 'Z': + return ['YES', cmd] + if not (visit[i][j] - 1 < len(move)): + continue + k1, k2 = move[visit[i][j] - 1] + + x, y = i + dir[k1][0], j + dir[k1][1] + if (0 <= x < h and 0 <= y < w) and arr[x][y] != '@': + visit[x][y] = visit[i][j] + 1 + q.append([x, y, cmd + k1]) + + x, y = i + dir[k2][0], j + dir[k2][1] + if (0 <= x < h and 0 <= y < w) and arr[x][y] != '@': + visit[x][y] = visit[i][j] + 1 + q.append([x, y, cmd + k2]) + return ['NO'] + + ans = solve(sx, sy) + for pp in ans: + print(pp) + + +if __name__ == '__main__': + main() From 64e5263bfeb83524421388517b57175fa7801308 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sat, 11 Nov 2023 02:30:43 +0900 Subject: [PATCH 06/50] =?UTF-8?q?[BOJ]=2020924:=20=ED=8A=B8=EB=A6=AC?= =?UTF-8?q?=EC=9D=98=20=EA=B8=B0=EB=91=A5=EA=B3=BC=20=EA=B0=80=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/20924.py | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 ddingmin/baekjoon/20924.py diff --git a/ddingmin/baekjoon/20924.py b/ddingmin/baekjoon/20924.py new file mode 100644 index 0000000..8d68c55 --- /dev/null +++ b/ddingmin/baekjoon/20924.py @@ -0,0 +1,48 @@ +import sys +from collections import deque + + +def main(): + input = sys.stdin.readline + n, root = map(int, input().split()) + + # init + answer = [None, 0] + adj = [[] for _ in range(n + 1)] + visit = [0] * (n + 1) + q = deque() + + for _ in range(n - 1): + a, b, dist = map(int, input().split()) + adj[a].append([dist, b]) + adj[b].append([dist, a]) + + q.append([0, root]) + visit[root] = 1 + + while q: + cur_dist, cur = q.popleft() + + # root -> leaf 최대 거리 갱신 + answer[1] = max(answer[1], cur_dist) + + # 기가노드를 판단하기 위해 count + count = 0 + for next_dist, next in adj[cur]: + if visit[next]: + continue + q.append([cur_dist + next_dist, next]) + visit[next] = 1 + count += 1 + if count > 1 and answer[0] == None: # 기가 노드를 식별 X 상태, 현재 가지로부터 2갈래로 퍼져나가는 경우 + # root -> 기가노드 거리 측정 + answer[0] = cur_dist + + # 기가 노드가 없는 경우 예외 처리 + if answer[0] == None: + answer[0] = answer[1] + print(answer[0], answer[1] - answer[0]) + + +if __name__ == '__main__': + main() From a949cfd76c8bc5f3808240c639d780e44805e584 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sat, 11 Nov 2023 19:25:06 +0900 Subject: [PATCH 07/50] =?UTF-8?q?[BOJ]=2014618:=20=EC=B4=9D=EA=B9=A1=20?= =?UTF-8?q?=EC=B4=9D=EA=B9=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/14618.py | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 ddingmin/baekjoon/14618.py diff --git a/ddingmin/baekjoon/14618.py b/ddingmin/baekjoon/14618.py new file mode 100644 index 0000000..0ed6394 --- /dev/null +++ b/ddingmin/baekjoon/14618.py @@ -0,0 +1,54 @@ +import sys +import heapq + + +def solve(adj, visit, a_type, b_type, start): + # dijk + visit[start] = 0 + hq = [[0, start]] + while hq: + dist, cur = heapq.heappop(hq) + if visit[cur] != dist: + continue + for next_dist, nxt in adj[cur]: + total_dist = dist + next_dist + if total_dist < visit[nxt]: + visit[nxt] = total_dist + heapq.heappush(hq, [total_dist, nxt]) + + # A형 집 부터 갱신 + answer = [None, float('inf')] + for node in a_type: + if visit[node] < answer[1]: + answer = ['A', visit[node]] + for node in b_type: + if visit[node] < answer[1]: + answer = ['B', visit[node]] + if answer[0] is None: + return [-1] + return answer + + +def main(): + input = sys.stdin.readline + n, m = map(int, input().split()) # node, edge + adj = [[] for _ in range(n + 1)] + visit = [float('inf')] * (n + 1) + + home_node = int(input()) + k = int(input()) # 종류 별 동물의 수 + a_type = list(map(int, input().split())) + b_type = list(map(int, input().split())) + + for _ in range(m): + a, b, dist = map(int, input().split()) + adj[a].append([dist, b]) + adj[b].append([dist, a]) + + ans = solve(adj, visit, a_type, b_type, home_node) + for pp in ans: + print(pp) + + +if __name__ == '__main__': + main() From 42aff2ebece6ccdb0319191fc3bf49f5bbf0da62 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Mon, 13 Nov 2023 01:21:57 +0900 Subject: [PATCH 08/50] =?UTF-8?q?[BOJ]=2025391:=20=ED=8A=B9=EB=B3=84?= =?UTF-8?q?=EC=83=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/25391.py | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 ddingmin/baekjoon/25391.py diff --git a/ddingmin/baekjoon/25391.py b/ddingmin/baekjoon/25391.py new file mode 100644 index 0000000..b985465 --- /dev/null +++ b/ddingmin/baekjoon/25391.py @@ -0,0 +1,40 @@ +import sys + + +def solve(n, m, k, arr): + answer = 0 + + arr.sort(key=lambda x: [-x[1], -x[0]]) + new_arr = [] + + for i in range(k): + a, b = arr[i] + answer += a + + for i in range(k, n): + a, b = arr[i] + new_arr.append([a, b]) + + new_arr.sort(key=lambda x: [-x[0], -x[1]]) + + for i in range(m): + a, b = new_arr[i] + answer += a + + return answer + + +def main(): + input = sys.stdin.readline + n, m, k = map(int, input().split()) + arr = [] + + for idx in range(n): + a, b = map(int, input().split()) + arr.append([a, b]) + + print(solve(n, m, k, arr)) + + +if __name__ == '__main__': + main() From 18d53c59ba0ca404ad63ba34e6dd5e839cd36161 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Mon, 13 Nov 2023 12:12:40 +0900 Subject: [PATCH 09/50] =?UTF-8?q?[BOJ]=2030108:=20=EA=B5=90=EC=9C=A1?= =?UTF-8?q?=EC=A0=81=EC=9D=B8=20=ED=8A=B8=EB=A6=AC=20=EB=AC=B8=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/30108.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 ddingmin/baekjoon/30108.py diff --git a/ddingmin/baekjoon/30108.py b/ddingmin/baekjoon/30108.py new file mode 100644 index 0000000..6a7b713 --- /dev/null +++ b/ddingmin/baekjoon/30108.py @@ -0,0 +1,36 @@ +import sys +import heapq + + +def solve(n, adj, node): + answer = [0] + start = 1 + + hq = [] + heapq.heappush(hq, [-1 * node[start - 1], start]) + + while hq: + dist, cur = heapq.heappop(hq) + answer.append((-1 * dist) + answer[-1]) + for nxt in adj[cur]: + heapq.heappush(hq, [-1 * node[nxt - 1], nxt]) + + return answer[1:] + + +def main(): + input = sys.stdin.readline + n = int(input()) + adj = [[] for _ in range(n + 1)] + p = list(map(int, input().split())) + for cur in range(len(p)): + adj[p[cur]].append(cur + 2) + node = list(map(int, input().split())) + + result = solve(n, adj, node) + for pp in result: + print(pp) + + +if __name__ == '__main__': + main() From bf8462f3c7d354d14dda7c0fa3a0a5ee8bc157b8 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Thu, 16 Nov 2023 21:28:42 +0900 Subject: [PATCH 10/50] [BOJ] 10429: QUENTO --- ddingmin/baekjoon/10429.py | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 ddingmin/baekjoon/10429.py diff --git a/ddingmin/baekjoon/10429.py b/ddingmin/baekjoon/10429.py new file mode 100644 index 0000000..49869b4 --- /dev/null +++ b/ddingmin/baekjoon/10429.py @@ -0,0 +1,61 @@ +import sys + + +def is_numeric(s): + try: + value = int(s) + return True + except ValueError: + return False + + +def solve(n, m, arr): + dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1] + visit = [[0] * 3 for _ in range(3)] + answer_cmd = None + + def backtracking(i, j, cmd, total, target, depth): + nonlocal answer_cmd + if depth == 0: + if total == target: + answer_cmd = str(cmd) + return + + for k in range(4): + x, y = i + dx[k], j + dy[k] + if not (0 <= x < 3 and 0 <= y < 3) or visit[x][y] == 1: + continue + visit[x][y] = 1 + if is_numeric(arr[x][y]): + if arr[i][j] == '+': + backtracking(x, y, cmd + f"\n{x} {y}", total + int(arr[x][y]), target, depth - 1) + else: + backtracking(x, y, cmd + f"\n{x} {y}", total - int(arr[x][y]), target, depth - 1) + else: + backtracking(x, y, cmd + f"\n{x} {y}", total, target, depth) + visit[x][y] = 0 + + sx, sy = [0, 0, 1, 2, 2], [0, 2, 1, 0, 2] + for s in range(5): + xx, yy = sx[s], sy[s] + visit[xx][yy] = 1 + backtracking(xx, yy, f"{xx} {yy}", int(arr[xx][yy]), n, m - 1) + visit[xx][yy] = 0 + return answer_cmd + + +def main(): + n, m = map(int, input().split()) + arr = [list(map(str, input().strip())) for _ in range(3)] + ans = solve(n, m, arr) + if ans is not None: + print(1) + print(ans, end = "") + else: + print(0) + + +if __name__ == '__main__': + sys.setrecursionlimit(10 ** 5) + input = sys.stdin.readline + main() From 09b7aa971c9f40173aa465dabfdd762d02c8813d Mon Sep 17 00:00:00 2001 From: ddingmin Date: Fri, 17 Nov 2023 00:10:03 +0900 Subject: [PATCH 11/50] =?UTF-8?q?[BOJ]=2016197:=20=EB=91=90=20=EB=8F=99?= =?UTF-8?q?=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/16197.py | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 ddingmin/baekjoon/16197.py diff --git a/ddingmin/baekjoon/16197.py b/ddingmin/baekjoon/16197.py new file mode 100644 index 0000000..e724358 --- /dev/null +++ b/ddingmin/baekjoon/16197.py @@ -0,0 +1,61 @@ +import sys +from collections import deque + + +def move(arr, n, m, i, j, x, y): + if not (0 <= x < n and 0 <= y < m): + return x, y + if arr[x][y] == '#': + return i, j + return x, y + + +def solve(n, m, arr, coins): + dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1] + q = deque() + q.append(coins) + visit = {} + visit[str(coins)] = 1 + + depth = 1 + + while q: + if depth > 10: + return -1 + for _ in range(len(q)): + coin = q.popleft() + i1, j1 = coin[0] + i2, j2 = coin[1] + for k in range(4): + x1, y1 = move(arr, n, m, i1, j1, i1 + dx[k], j1 + dy[k]) + x2, y2 = move(arr, n, m, i2, j2, i2 + dx[k], j2 + dy[k]) + nxt = [[x1, y1], [x2, y2]] + if str(nxt) in visit: continue + if not (0 <= x1 < n and 0 <= y1 < m) and not (0 <= x2 < n and 0 <= y2 < m): + continue + + if (0 <= x1 < n and 0 <= y1 < m) and (0 <= x2 < n and 0 <= y2 < m): + visit[str(nxt)] = 1 + q.append(nxt) + else: + return depth + depth += 1 + return -1 + + +def main(): + n, m = map(int, input().split()) + arr = [list(map(str, input().strip())) for _ in range(n)] + coins = [] + for i in range(n): + for j in range(m): + if arr[i][j] == 'o': + coins.append([i, j]) + + print(solve(n, m, arr, coins)) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 5) + input = sys.stdin.readline + main() From 6894907e65a0ca7280db61a4a5c527d475efc931 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sat, 18 Nov 2023 02:32:00 +0900 Subject: [PATCH 12/50] =?UTF-8?q?[BOJ]=209694:=20=EB=AC=B4=EC=97=87?= =?UTF-8?q?=EC=9D=84=20=EC=95=84=EB=8A=90=EB=83=90=EA=B0=80=20=EC=95=84?= =?UTF-8?q?=EB=8B=88=EB=9D=BC=20=EB=88=84=EA=B5=AC=EB=A5=BC=20=EC=95=84?= =?UTF-8?q?=EB=8A=90=EB=83=90=EA=B0=80=20=EB=AC=B8=EC=A0=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/9694.py | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 ddingmin/baekjoon/9694.py diff --git a/ddingmin/baekjoon/9694.py b/ddingmin/baekjoon/9694.py new file mode 100644 index 0000000..3de4583 --- /dev/null +++ b/ddingmin/baekjoon/9694.py @@ -0,0 +1,54 @@ +import sys +import heapq + + +def solve(n, m, adj, costs): + prev = [0] * (m + 1) + costs[m] = 0 + hq = [[0, m]] + while hq: + cost, cur = heapq.heappop(hq) + if costs[cur] != cost: + continue + for nxt, nxt_cost in adj[cur]: + temp = cost + nxt_cost + if temp < costs[nxt]: + costs[nxt] = temp + heapq.heappush(hq, [temp, nxt]) + prev[nxt] = cur + + if costs[m - 1] == float('inf'): + return -1 + + find = [] + find.append(m - 1) + + f = m - 1 + while f != m: + f = prev[f] + find.append(f) + find[-1] = 0 + return " ".join(map(str, find[::-1])) + + +def main(): + t = int(input()) + for idx in range(1, t + 1): + n, m = map(int, input().split()) + adj = [[] for _ in range(m + 1)] + costs = [float('inf')] * (m + 1) + for _ in range(n): + a, b, c = map(int, input().split()) + if a == 0: + a = m + if b == 0: + b = m + adj[a].append([b, c]) + adj[b].append([a, c]) + print(f"Case #{idx}: {solve(n, m, adj, costs)}") + + +if __name__ == '__main__': + sys.setrecursionlimit(10 ** 5) + input = sys.stdin.readline + main() From 67ea79cf6591c038e27a4413064ef6ed535044b3 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sat, 18 Nov 2023 11:50:34 +0900 Subject: [PATCH 13/50] =?UTF-8?q?[BOJ]=2015809:=20=EC=A0=84=EA=B5=AD?= =?UTF-8?q?=EC=8B=9C=EB=8C=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/15809.py | 67 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 ddingmin/baekjoon/15809.py diff --git a/ddingmin/baekjoon/15809.py b/ddingmin/baekjoon/15809.py new file mode 100644 index 0000000..e8736f9 --- /dev/null +++ b/ddingmin/baekjoon/15809.py @@ -0,0 +1,67 @@ +import sys + + +def find(parents, cur): + if parents[cur] == -1: + return -1 + if parents[cur] == cur: + return cur + parents[cur] = find(parents, parents[cur]) + return parents[cur] + + +def union(parents, nodes, a, b): + ap = find(parents, a) + bp = find(parents, b) + parents[bp] = ap + nodes[ap] += nodes[bp] + nodes[bp] = 0 + + +def fight(parents, nodes, a, b): + ap = find(parents, a) + bp = find(parents, b) + if nodes[ap] < nodes[bp]: + ap, bp = bp, ap + elif nodes[ap] == nodes[bp]: + parents[ap], parents[bp] = -1, -1 + parents[bp] = ap + nodes[ap] -= nodes[bp] + nodes[bp] = 0 + + +def solve(n, m, nodes, parents, cmds): + for cmd, p, q in cmds: + p, q = p - 1, q - 1 + if cmd == 1: + union(parents, nodes, p, q) + else: + fight(parents, nodes, p, q) + visit = [0] * n + answer = [] + for node in range(n): + p = find(parents, node) + if p == -1 or visit[p]: continue + visit[p] = 1 + answer.append(nodes[p]) + + ans = str(len(answer)) + "\n" + ans += " ".join(map(str, sorted(answer))) + return ans + + +def main(): + n, m = map(int, input().split()) + nodes = [] + parents = [] + for i in range(n): + nodes.append(int(input())) + parents.append(i) + cmds = [list(map(int, input().split())) for _ in range(m)] + print(solve(n, m, nodes, parents, cmds)) + + +if __name__ == '__main__': + sys.setrecursionlimit(10 ** 5) + input = sys.stdin.readline + main() From eb0e97a135b9a5f2a6fb2a74a8c15fd42130932c Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sat, 18 Nov 2023 17:45:59 +0900 Subject: [PATCH 14/50] [BOJ] 27942: :danceplant: --- ddingmin/baekjoon/27942.py | 79 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 ddingmin/baekjoon/27942.py diff --git a/ddingmin/baekjoon/27942.py b/ddingmin/baekjoon/27942.py new file mode 100644 index 0000000..b13731f --- /dev/null +++ b/ddingmin/baekjoon/27942.py @@ -0,0 +1,79 @@ +import sys +from collections import deque + +dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1] + + +def grow(arr, k, sx, sy): + if k == 0: + sx[0] -= 1 + for k in range(sy[0], sy[1] + 1): + arr[sx[0]][k] = 0 + return 'U' + elif k == 1: + sx[1] += 1 + for k in range(sy[0], sy[1] + 1): + arr[sx[1]][k] = 0 + return 'D' + elif k == 2: + sy[0] -= 1 + for k in range(sx[0], sx[1] + 1): + arr[k][sy[0]] = 0 + return 'L' + else: + sy[1] += 1 + for k in range(sx[0], sx[1] + 1): + arr[k][sy[1]] = 0 + return 'R' + + +def get_start_ij(k, sx, sy): + if k == 0: + return sx[0], sy[0], sx[0] + 1, sy[1] + 1 + elif k == 1: + return sx[1], sy[0], sx[1] + 1, sy[1] + 1 + elif k == 2: + return sx[0], sy[0], sx[1] + 1, sy[0] + 1 + else: + return sx[0], sy[1], sx[1] + 1, sy[1] + 1 + + +def solve(n, arr): + sx, sy = [n // 2 - 1, n // 2], [n // 2 - 1, n // 2] + cmd = "" + energy = 0 + + while 1: + eat = [0, 0, 0, 0] + for k in range(4): + si, sj, ei, ej = get_start_ij(k, sx, sy) + for i in range(si, ei): + for j in range(sj, ej): + x, y = i + dx[k], j + dy[k] + if not (0 <= x < n and 0 <= y < n): + continue + eat[k] += arr[x][y] + + target = [-1, None] + for k in range(4): + if eat[k] > 0 and target[0] < eat[k]: + target = [eat[k], k] + + if target[1] is None: + print(energy) + return cmd + + energy += target[0] + cmd += grow(arr, target[1], sx, sy) + + +def main(): + n = int(input()) + arr = [list(map(int, input().split())) for _ in range(n)] + print(solve(n, arr)) + + +if __name__ == '__main__': + sys.setrecursionlimit(10 ** 5) + input = sys.stdin.readline + main() From 5d93c81b85f73f71d954f63a8828bf3025397881 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Mon, 20 Nov 2023 19:11:55 +0900 Subject: [PATCH 15/50] =?UTF-8?q?[BOJ]=204920:=20=ED=85=8C=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=EC=8A=A4=20=EA=B2=8C=EC=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/4920.py | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 ddingmin/baekjoon/4920.py diff --git a/ddingmin/baekjoon/4920.py b/ddingmin/baekjoon/4920.py new file mode 100644 index 0000000..c541d32 --- /dev/null +++ b/ddingmin/baekjoon/4920.py @@ -0,0 +1,57 @@ +import sys + + +def solve(n, arr): + ans = -1 * float('inf') + + # 3개짜리 + for i in range(n - 1): + for j in range(n - 2): + ans = max(ans, arr[i][j] + arr[i][j + 1] + arr[i + 1][j + 1] + arr[i + 1][j + 2]) + + ans = max(ans, arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 2]) + ans = max(ans, arr[i + 1][j] + arr[i + 1][j + 1] + arr[i + 1][j + 2] + arr[i][j]) + + ans = max(ans, arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1]) + ans = max(ans, arr[i + 1][j] + arr[i + 1][j + 1] + arr[i + 1][j + 2] + arr[i][j + 1]) + + for i in range(n - 2): + for j in range(n - 1): + ans = max(ans, arr[i][j + 1] + arr[i + 1][j] + arr[i + 1][j + 1] + arr[i + 2][j]) + + ans = max(ans, arr[i][j] + arr[i + 1][j] + arr[i + 2][j] + arr[i + 1][j + 1]) + ans = max(ans, arr[i][j] + arr[i + 1][j] + arr[i + 2][j] + arr[i][j + 1]) + + ans = max(ans, arr[i][j + 1] + arr[i + 1][j + 1] + arr[i + 2][j + 1] + arr[i + 1][j]) + ans = max(ans, arr[i][j + 1] + arr[i + 1][j + 1] + arr[i + 2][j + 1] + arr[i + 2][j]) + + # 4개짜리 + for i in range(n - 1): + for j in range(n - 1): + ans = max(ans, arr[i][j] + arr[i + 1][j] + arr[i][j + 1] + arr[i + 1][j + 1]) + # 기다란거 + for i in range(n - 3): + for j in range(n): + ans = max(ans, arr[i][j] + arr[i + 1][j] + arr[i + 2][j] + arr[i + 3][j]) + for i in range(n): + for j in range(n - 3): + ans = max(ans, arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i][j + 3]) + + return ans + + +def main(): + idx = 1 + while 1: + n = int(input()) + if n == 0: + break + arr = [list(map(int, input().split())) for _ in range(n)] + print(f"{idx}. {solve(n, arr)}") + idx += 1 + + +if __name__ == '__main__': + sys.setrecursionlimit(10 ** 5) + input = sys.stdin.readline + main() From 61f655580e3c053029ce7de4caacf518c5b17d75 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 21 Nov 2023 00:03:40 +0900 Subject: [PATCH 16/50] =?UTF-8?q?[BOJ]=2014722:=20=EC=9A=B0=EC=9C=A0=20?= =?UTF-8?q?=EB=8F=84=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/14722.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ddingmin/baekjoon/14722.py diff --git a/ddingmin/baekjoon/14722.py b/ddingmin/baekjoon/14722.py new file mode 100644 index 0000000..dc11d7f --- /dev/null +++ b/ddingmin/baekjoon/14722.py @@ -0,0 +1,26 @@ +import sys + + +def solve(n, arr): + dp = [[0] * n for _ in range(n)] + + for i in range(n): + for j in range(n): + mDp = max(dp[i - 1][j], dp[i][j - 1]) + if arr[i][j] == mDp % 3: + dp[i][j] = mDp + 1 + else: + dp[i][j] = mDp + + return dp[-1][-1] + + +def main(): + n = int(input()) + arr = [list(map(int, input().split())) for _ in range(n)] + print(solve(n, arr)) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From 4b4a38a1031640b05fac9909b7d4424a0c90c0a1 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 21 Nov 2023 17:17:03 +0900 Subject: [PATCH 17/50] =?UTF-8?q?[BOJ]=2012886:=20=EB=8F=8C=20=EA=B7=B8?= =?UTF-8?q?=EB=A3=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/12886.py | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 ddingmin/baekjoon/12886.py diff --git a/ddingmin/baekjoon/12886.py b/ddingmin/baekjoon/12886.py new file mode 100644 index 0000000..001a69b --- /dev/null +++ b/ddingmin/baekjoon/12886.py @@ -0,0 +1,48 @@ +import sys +from collections import deque + + +def calc(x, y): + if x < y: + return x + x, y - x + else: + return x - y, y + y + + +def get_abc(i, j, k): + result = [] + a, b = calc(i, j) + result.append([a, b, k]) + b, c = calc(j, k) + result.append([i, b, c]) + a, c = calc(i, k) + result.append([a, j, c]) + return result + + +def solve(a, b, c): + visit = {} + visit[a, b, c] = True + q = deque() + q.append([a, b, c]) + + while q: + i, j, k = q.popleft() + if i == j == k: + return 1 + for a, b, c in get_abc(i, j, k): + if (a, b, c) in visit: continue + q.append([a, b, c]) + visit[a, b, c] = True + return 0 + + +def main(): + a, b, c = map(int, input().split()) + print(solve(a, b, c)) + + +if __name__ == '__main__': + sys.setrecursionlimit(10 ** 5) + input = sys.stdin.readline + main() From 4cabdd761174b45cb0c262905c4eac814e50336a Mon Sep 17 00:00:00 2001 From: ddingmin Date: Wed, 22 Nov 2023 22:50:23 +0900 Subject: [PATCH 18/50] =?UTF-8?q?[BOJ]=2019538:=20=EB=A3=A8=EB=A8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/19538.kt | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 ddingmin/baekjoon/19538.kt diff --git a/ddingmin/baekjoon/19538.kt b/ddingmin/baekjoon/19538.kt new file mode 100644 index 0000000..57a8262 --- /dev/null +++ b/ddingmin/baekjoon/19538.kt @@ -0,0 +1,62 @@ +import java.io.BufferedWriter +import java.io.OutputStreamWriter + +fun main() = with(System.`in`.bufferedReader()) { + val sout = BufferedWriter(OutputStreamWriter(System.out)) + var n = readLine().toInt() + var adj = Array(n) { + ArrayList() + } + repeat(n) { + readLine().split(" ").map { num -> + if (num.toInt() > 0) adj[it].add(num.toInt() - 1) + } + } + var m = readLine().toInt() + var start = readLine().split(" ").map { it.toInt() - 1 } + sout.appendLine(solve(n, adj, start)) + sout.close() +} + +fun solve(n: Int, adj: Array>, start: List): String { + var answer = IntArray(n) { -1 } + var count = IntArray(n) + var visit = BooleanArray(n) + + start.map { + answer[it] = 0 + visit[it] = true + adj[it].map { nxt -> + count[nxt] += 1 + } + } + + var dq = ArrayDeque(start) + + var time = 1 + while (dq.isNotEmpty()) { + var temp = ArrayList() + for (i in 1..dq.size) { + var cur = dq.removeFirst() + + for (nxt in adj[cur]) { + if (visit[nxt]) continue + if (count[nxt] * 2 >= adj[nxt].size) { + visit[nxt] = true + dq.add(nxt) + answer[nxt] = time + temp.add(nxt) + } + } + + } + temp.map { + adj[it].map { nxt -> + count[nxt] += 1 + } + } + time += 1 + } + + return answer.joinToString(" ") +} From 7cfa83f5d3b753150d7fa64282bf00c205513144 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Fri, 24 Nov 2023 02:57:14 +0900 Subject: [PATCH 19/50] =?UTF-8?q?[BOJ]=2010710:=20=EC=8B=A4=ED=81=AC?= =?UTF-8?q?=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/10710.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 ddingmin/baekjoon/10710.py diff --git a/ddingmin/baekjoon/10710.py b/ddingmin/baekjoon/10710.py new file mode 100644 index 0000000..5b056a3 --- /dev/null +++ b/ddingmin/baekjoon/10710.py @@ -0,0 +1,24 @@ +import sys + + +def solve(n, m, city, arr): + dp = [[float('inf')] * (n + 1) for _ in range(m + 1)] + dp[0][0] = 0 + + for i in range(1, m + 1): # 일 + for j in range(n + 1): # 도시 + dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1] + city[j - 1] * arr[i - 1]) + + return dp[-1][-1] + + +def main(): + n, m = map(int, input().split()) + city = list(int(input()) for _ in range(n)) + arr = list(int(input()) for _ in range(m)) + print(solve(n, m, city, arr)) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From f1caa72d817225a365f0e1339a8e83e773e36c68 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Fri, 24 Nov 2023 12:17:51 +0900 Subject: [PATCH 20/50] =?UTF-8?q?[BOJ]=2014658:=20=ED=95=98=EB=8A=98?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EB=B3=84=EB=98=A5=EB=B3=84=EC=9D=B4=20?= =?UTF-8?q?=EB=B9=97=EB=B0=9C=EC=B9=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/14658.kt | 40 ++++++++++++++++++++++++++++++++++++++ ddingmin/baekjoon/14658.py | 33 +++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 ddingmin/baekjoon/14658.kt create mode 100644 ddingmin/baekjoon/14658.py diff --git a/ddingmin/baekjoon/14658.kt b/ddingmin/baekjoon/14658.kt new file mode 100644 index 0000000..d39525f --- /dev/null +++ b/ddingmin/baekjoon/14658.kt @@ -0,0 +1,40 @@ +import java.io.BufferedWriter +import java.io.OutputStreamWriter + +fun main() = with(System.`in`.bufferedReader()) { + val sout = BufferedWriter(OutputStreamWriter(System.out)) + + var line = readLine().split(" ").map { it.toInt() } + var l = line[2] + var k = line[3] + + var arr = Array(k) { + readLine().split(" ").map { it.toInt() } + } + + sout.appendLine(solve(l, k, arr)) + sout.close() +} + +fun solve(l: Int, k: Int, arr: Array>): String { + var count = 0 + + for (star1 in arr) { + for (star2 in arr) { + var sx = minOf(star1[0], star2[0]) + var sy = minOf(star1[1], star2[1]) + var cnt = 0 + + for (star in arr) { + if (sx <= star[0] && star[0] <= sx + l && + sy <= star[1] && star[1] <= sy + l + ) { + cnt += 1 + } + } + count = maxOf(count, cnt) + } + } + + return (k - count).toString() +} diff --git a/ddingmin/baekjoon/14658.py b/ddingmin/baekjoon/14658.py new file mode 100644 index 0000000..6a2aaaa --- /dev/null +++ b/ddingmin/baekjoon/14658.py @@ -0,0 +1,33 @@ +import sys + + +def solve(n, m, l, k, arr): + answer = 0 + for i in range(k): + for j in range(k): + cnt = 0 + + # 두 별을 걸치는 가장 왼쪽 상단 좌표 + sx, sy = min(arr[i][0], arr[j][0]), min(arr[i][1], arr[j][1]) + + for x, y in arr: + # 구한 좌표의 최대 범위 별 개수 구하기 + if sx <= x <= sx + l and sy <= y <= sy + l: + cnt += 1 + answer = max(answer, cnt) + + return k - answer + + +def main(): + n, m, l, k = map(int, input().split()) + arr = [] + for _ in range(k): + arr.append(list(map(int, input().split()))) + print(solve(n, m, l, k, arr)) + + +if __name__ == '__main__': + sys.setrecursionlimit(10 ** 5) + input = sys.stdin.readline + main() From a6748d65ae4f7f64622cbf3cd9bf40296e7b067a Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sun, 26 Nov 2023 02:35:17 +0900 Subject: [PATCH 21/50] =?UTF-8?q?[BOJ]=203221:=20=EA=B0=9C=EB=AF=B8?= =?UTF-8?q?=EC=9D=98=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/3221.py | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ddingmin/baekjoon/3221.py diff --git a/ddingmin/baekjoon/3221.py b/ddingmin/baekjoon/3221.py new file mode 100644 index 0000000..1cae0be --- /dev/null +++ b/ddingmin/baekjoon/3221.py @@ -0,0 +1,42 @@ +import sys + + +def move(direction, cur, l, t): + if direction == 'L': + temp = t - cur + if temp < 0: + return cur - t + # 홀수 + if temp // l % 2 == 1: + return l - temp % l + else: + return temp % l + else: + temp = t - (l - cur) + if temp < 0: + return cur + t + if temp // l % 2 == 0: + return l - temp % l + else: + return temp % l + + + +def solve(l, t, cmd): + answer = [] + for cur, c in cmd: + answer.append(move(c, int(cur), l, t)) + return sorted(answer) + + +def main(): + l, t = map(int, input().split()) + n = int(input()) + cmd = list(input().split() for _ in range(n)) + print(*solve(l, t, cmd)) + + +if __name__ == '__main__': + sys.setrecursionlimit(10 ** 5) + input = sys.stdin.readline + main() From 5ba05c1b1183cce063d80e7a849ad029ffff69db Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 28 Nov 2023 15:28:34 +0900 Subject: [PATCH 22/50] =?UTF-8?q?[BOJ]=206086:=20=EC=B5=9C=EB=8C=80=20?= =?UTF-8?q?=EC=9C=A0=EB=9F=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/17412.py | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 ddingmin/baekjoon/17412.py diff --git a/ddingmin/baekjoon/17412.py b/ddingmin/baekjoon/17412.py new file mode 100644 index 0000000..7c043a1 --- /dev/null +++ b/ddingmin/baekjoon/17412.py @@ -0,0 +1,62 @@ +import sys +from collections import deque + +MAX_NODE = 401 + + +def solve(capacity, flow, adj, start, end): + ans = 0 + visit = [[0] * MAX_NODE for _ in range(MAX_NODE)] + + while 1: + prev = [0] * MAX_NODE + q = deque([start]) + while q and prev[end] == 0: + cur = q.popleft() + for nxt in adj[cur]: + if not visit[cur][nxt] and not prev[nxt] and capacity[cur][nxt] - flow[cur][nxt] > 0: + prev[nxt] = cur + q.append(nxt) + if nxt == end: + break + + if prev[end] == 0: + break + + min_flow = float('inf') + + node = end + while node != start: + min_flow = min(min_flow, capacity[prev[node]][node] - flow[prev[node]][node]) + node = prev[node] + + node = end + while node != start: + flow[prev[node]][node] += 1 + flow[node][prev[node]] -= 1 + visit[prev[node]][node] = 1 + node = prev[node] + + ans += 1 + + return ans + + +def main(): + n, p = map(int, input().split()) + capacity = [[0] * MAX_NODE for _ in range(MAX_NODE)] + flow = [[0] * MAX_NODE for _ in range(MAX_NODE)] + adj = [[] for _ in range(MAX_NODE)] + for _ in range(p): + a, b = map(int, input().split()) + capacity[a][b] = 1 + adj[a].append(b) + adj[b].append(a) + + print(solve(capacity, flow, adj, 1, 2)) + + +if __name__ == '__main__': + sys.setrecursionlimit(10 ** 5) + input = sys.stdin.readline + main() From 4375a8e6594c127f8cbaf49d3a56e177ca3faf3e Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 28 Nov 2023 15:34:59 +0900 Subject: [PATCH 23/50] =?UTF-8?q?[BOJ]=206086:=20=EC=B5=9C=EB=8C=80=20?= =?UTF-8?q?=EC=9C=A0=EB=9F=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 문제 번호 수정 --- ddingmin/baekjoon/{17412.py => 6086.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ddingmin/baekjoon/{17412.py => 6086.py} (100%) diff --git a/ddingmin/baekjoon/17412.py b/ddingmin/baekjoon/6086.py similarity index 100% rename from ddingmin/baekjoon/17412.py rename to ddingmin/baekjoon/6086.py From 7234c5aa5daa2c472c5b7a59e25fab62b5817054 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 28 Nov 2023 16:10:17 +0900 Subject: [PATCH 24/50] =?UTF-8?q?[BOJ]=2028291:=20=EB=A0=88=EB=93=9C?= =?UTF-8?q?=EC=8A=A4=ED=86=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/28291.py | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 ddingmin/baekjoon/28291.py diff --git a/ddingmin/baekjoon/28291.py b/ddingmin/baekjoon/28291.py new file mode 100644 index 0000000..4595fc1 --- /dev/null +++ b/ddingmin/baekjoon/28291.py @@ -0,0 +1,54 @@ +import sys +from collections import deque + + +def solve(n, m, arr, power, lamps): + dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1] + q = deque(lamps) + while q: + i, j = q.popleft() + for k in range(4): + x, y = i + dx[k], j + dy[k] + if not (0 <= x < n and 0 <= y < m): continue + if arr[x][y] == 1 and power[i][j] - 1 > power[x][y]: + power[x][y] = power[i][j] - 1 + q.append([x, y]) + + for i in range(n): + for j in range(m): + if arr[i][j] == 3: + flag = False + for k in range(4): + x, y = i + dx[k], j + dy[k] + if not (0 <= x < n and 0 <= y < m): + continue + if power[x][y]: + flag = True + if not flag: + return "failed" + + return "success" + + +def main(): + n, m = map(int, input().split()) + arr = [[0] * m for _ in range(n)] + power = [[0] * m for _ in range(n)] + lamps = [] + for _ in range(int(input())): + cmd, i, j = input().split() + i, j = int(i), int(j) + if cmd == 'redstone_block': + arr[i][j] = 2 + lamps.append([i, j]) + power[i][j] = 15 + elif cmd == 'redstone_dust': + arr[i][j] = 1 + elif cmd == 'redstone_lamp': + arr[i][j] = 3 + print(solve(n, m, arr, power, lamps)) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From 52b6149b18437a61e1b53c7d711994a38495ae1d Mon Sep 17 00:00:00 2001 From: ddingmin Date: Wed, 29 Nov 2023 22:35:19 +0900 Subject: [PATCH 25/50] =?UTF-8?q?[BOJ]=2014925:=20=EB=AA=A9=EC=9E=A5=20?= =?UTF-8?q?=EA=B1=B4=EC=84=A4=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/14925.kt | 31 +++++++++++++++++++++++++++++++ ddingmin/baekjoon/14925.py | 25 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 ddingmin/baekjoon/14925.kt create mode 100644 ddingmin/baekjoon/14925.py diff --git a/ddingmin/baekjoon/14925.kt b/ddingmin/baekjoon/14925.kt new file mode 100644 index 0000000..da0e1bb --- /dev/null +++ b/ddingmin/baekjoon/14925.kt @@ -0,0 +1,31 @@ +import java.io.BufferedWriter +import java.io.OutputStreamWriter + +fun main() = with(System.`in`.bufferedReader()) { + val sout = BufferedWriter(OutputStreamWriter(System.out)) + + var nm = readLine().split(" ").map { it.toInt() } + + var arr = Array(nm[0]) { + readLine().split(" ").map { it.toInt() } + } + + sout.appendLine(solve(nm, arr)) + sout.close() +} + +fun solve(nm: List, arr: Array>): String { + var ans = 0 + var dp = Array(nm[0] + 1) { + IntArray(nm[1] + 1) + } + + for (i in 1..nm[0]) { + for (j in 1..nm[1]) { + if (arr[i - 1][j - 1] == 0) dp[i][j] = minOf(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1 + ans = maxOf(ans, dp[i][j]) + } + } + + return ans.toString() +} diff --git a/ddingmin/baekjoon/14925.py b/ddingmin/baekjoon/14925.py new file mode 100644 index 0000000..d372112 --- /dev/null +++ b/ddingmin/baekjoon/14925.py @@ -0,0 +1,25 @@ +import sys + + +def solve(n, m, arr): + dp = [[0] * m for _ in range(n)] + ans = 0 + + for i in range(n): + for j in range(m): + if arr[i][j] == 0: + dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1 + ans = max(ans, dp[i][j]) + + return ans + + +def main(): + n, m = map(int, input().split()) + arr = [list(map(int, input().split())) for _ in range(n)] + print(solve(n, m, arr)) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From 02c73c7054d0c79203d70f1578951ce902825130 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Thu, 30 Nov 2023 13:44:23 +0900 Subject: [PATCH 26/50] [BOJ] 4650: Jungle Roads --- ddingmin/baekjoon/4650.py | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 ddingmin/baekjoon/4650.py diff --git a/ddingmin/baekjoon/4650.py b/ddingmin/baekjoon/4650.py new file mode 100644 index 0000000..9a3cdd5 --- /dev/null +++ b/ddingmin/baekjoon/4650.py @@ -0,0 +1,62 @@ +import sys +import heapq + + +def to_node(node_dict, char): + if char not in node_dict: + node_dict[char] = len(node_dict) + return node_dict[char] + + +def solve(n, adj): + start = 0 + visit = [0] * n + visited_cnt = 1 + visit[start] = 1 + ans = 0 + + hq = [] + for nxt, cost in adj[start]: + heapq.heappush(hq, [cost, nxt]) + + while visited_cnt < n and hq: + cost, cur = heapq.heappop(hq) + if visit[cur]: continue + visit[cur] = 1 + visited_cnt += 1 + ans += cost + for nxt, nxt_cost in adj[cur]: + heapq.heappush(hq, [nxt_cost, nxt]) + + return ans + + +def main(): + n = int(input()) + lines = [] + + while 1: + if n == 0: + break + line = list(input().split()) + if len(line) == 1: + node_dict = {} + adj = [[] for _ in range(n)] + for l in lines: + if len(l) > 2: + a = to_node(node_dict, l[0]) + for i in range(1, len(l) // 2): + b = to_node(node_dict, l[i * 2]) + c = int(l[i * 2 + 1]) + adj[a].append([b, c]) + adj[b].append([a, c]) + print(solve(n, adj)) + n = int(line[0]) + lines.clear() + else: + lines.append(line) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From 931398c2cfeef718a18558394175dab8808b9511 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sat, 2 Dec 2023 03:30:07 +0900 Subject: [PATCH 27/50] =?UTF-8?q?[BOJ]=202655:=20=EA=B0=80=EC=9E=A5?= =?UTF-8?q?=EB=86=92=EC=9D=80=ED=83=91=EC=8C=93=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/2655.py | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 ddingmin/baekjoon/2655.py diff --git a/ddingmin/baekjoon/2655.py b/ddingmin/baekjoon/2655.py new file mode 100644 index 0000000..02e0a93 --- /dev/null +++ b/ddingmin/baekjoon/2655.py @@ -0,0 +1,43 @@ +import sys + + +def solve(n, arr): + # 넓이, 무게, 번호, 높이 + lis = [0] * n + ans = [0] * n + top = [0] * n + + for i in range(n): + lis[i] = 1 + ans[i] = [arr[i][2]] + top[i] = arr[i][3] + for j in range(i): + if arr[j][1] > arr[i][1]: + if top[i] < top[j] + arr[i][3]: + top[i] = top[j] + arr[i][3] + lis[i] = lis[j] + 1 + ans[i] = ans[j] + [arr[i][2]] + + answer = [0, 0, []] + for i in range(n): + if answer[0] < top[i]: + answer = [top[i], lis[i], ans[i]] + + print(answer[1]) + for pp in answer[2][::-1]: + print(pp) + return 0 + + +def main(): + n = int(input()) + arr = [] + for idx in range(1, n + 1): + a, b, c = map(int, input().split()) + arr.append([a, c, idx, b]) + solve(n, sorted(arr, key=lambda x: [-x[0], x[1]])) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From 574af2daa8cdcb05747657e4444406c6a31e526b Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sun, 3 Dec 2023 12:34:42 +0900 Subject: [PATCH 28/50] =?UTF-8?q?[BOJ]=207983:=20=EB=82=B4=EC=9D=BC=20?= =?UTF-8?q?=ED=95=A0=EA=B1=B0=EC=95=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/7983.kt | 28 ++++++++++++++++++++++++++++ ddingmin/baekjoon/7983.py | 20 ++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 ddingmin/baekjoon/7983.kt create mode 100644 ddingmin/baekjoon/7983.py diff --git a/ddingmin/baekjoon/7983.kt b/ddingmin/baekjoon/7983.kt new file mode 100644 index 0000000..79dbb09 --- /dev/null +++ b/ddingmin/baekjoon/7983.kt @@ -0,0 +1,28 @@ +import java.io.BufferedWriter +import java.io.OutputStreamWriter + +fun main() = with(System.`in`.bufferedReader()) { + val sout = BufferedWriter(OutputStreamWriter(System.out)) + + var n = readLine().toInt() + var arr = Array(n) { + readLine().split(" ").map { it.toLong() } + } + + sout.appendLine(solve(n, arr)) + sout.close() +} + +fun solve(n: Int, arr: Array>): String { + var ans = Long.MAX_VALUE + arr.sortBy { -it[1] } + + for (values in arr) { + var doing = values[0] + var deadline = values[1] + ans = minOf(ans, deadline) - doing + } + + + return ans.toString() +} diff --git a/ddingmin/baekjoon/7983.py b/ddingmin/baekjoon/7983.py new file mode 100644 index 0000000..ff78b5e --- /dev/null +++ b/ddingmin/baekjoon/7983.py @@ -0,0 +1,20 @@ +import sys + + +def solve(n, arr): + ans = float('inf') + for doing, deadline in arr: + ans = min(ans, deadline) - doing + + return ans + + +def main(): + n = int(input()) + arr = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: [-x[1]]) + print(solve(n, arr)) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From f494f4b5a592590521561153011064da70f0e851 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Mon, 4 Dec 2023 20:07:42 +0900 Subject: [PATCH 29/50] =?UTF-8?q?[BOJ]=209944:=20NxM=20=EB=B3=B4=EB=93=9C?= =?UTF-8?q?=20=EC=99=84=EC=A3=BC=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/9944.py | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 ddingmin/baekjoon/9944.py diff --git a/ddingmin/baekjoon/9944.py b/ddingmin/baekjoon/9944.py new file mode 100644 index 0000000..e5af225 --- /dev/null +++ b/ddingmin/baekjoon/9944.py @@ -0,0 +1,79 @@ +import sys + + +def solve(n, m, arr, need_filled): + dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1] + + def bt(i, j, filled_count, depth): + nonlocal need_filled + temp = float('inf') + + # 이미 최소 경로를 넘은 경우 + if depth > ans: + return temp + + # 모든 공간이 채워진 경우 + if filled_count == need_filled: + return depth + + for k in range(4): + x, y = i + dx[k], j + dy[k] + if not (0 <= x < n and 0 <= y < m): continue + if not visit[x][y] and arr[x][y] == '.': + f_cnt = 0 + + fill_arr = [] + while 1: + fill_arr.append([x, y]) + visit[x][y] = 1 + f_cnt += 1 + nx, ny = x + dx[k], y + dy[k] + if not (0 <= nx < n and 0 <= ny < m): break + if not visit[nx][ny] and arr[nx][ny] == '.': + x, y = nx, ny + else: + break + + temp = min(temp, bt(x, y, filled_count + f_cnt, depth + 1)) + + clear_arr = [] + # 되돌리기 + for idx in range(1, f_cnt + 1): + clear_arr.append([i + dx[k] * idx, j + dy[k] * idx]) + visit[i + dx[k] * idx][j + dy[k] * idx] = 0 + + return temp + + ans = float('inf') + for i in range(n): + for j in range(m): + if arr[i][j] == '.': + visit = [[0] * m for _ in range(n)] + visit[i][j] = 1 + ans = min(ans, bt(i, j, 1, 0)) + if ans == float('inf'): + ans = -1 + return ans + + +def main(): + case = 1 + while True: + try: + n, m = map(int, input().split()) + arr = [list(input().strip()) for _ in range(n)] + need_filled = 0 + for i in range(n): + for j in range(m): + if arr[i][j] == '.': + need_filled += 1 + print(f"Case {case}: {solve(n, m, arr, need_filled)}") + case += 1 + except EOFError: + break + + +if __name__ == '__main__': + sys.setrecursionlimit(10 ** 5) + # input = sys.stdin.readline + main() From e5f92c6c70b7c9b4700b4f5874cc506dfd97f15b Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 5 Dec 2023 16:09:22 +0900 Subject: [PATCH 30/50] =?UTF-8?q?[BOJ]=2017305:=20=EC=82=AC=ED=83=95=20?= =?UTF-8?q?=EB=B0=B0=EB=8B=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 17305.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 17305.py diff --git a/17305.py b/17305.py new file mode 100644 index 0000000..b4087fd --- /dev/null +++ b/17305.py @@ -0,0 +1,39 @@ +import sys + + +def solve(n, m, three, five): + pSumThree = [0] + pSumFive = [0] + + for w, num in three: + pSumThree.append(pSumThree[-1] + num) + for w, num in five: + pSumFive.append(pSumFive[-1] + num) + + ans = 0 + + for cnt in range(len(pSumThree)): + if cnt * 3 > m: break + availableFiveCount = min((m - cnt * 3) // 5, len(pSumFive) - 1) + ans = max(ans, pSumThree[cnt] + pSumFive[availableFiveCount]) + + return ans + + +def main(): + n, m = map(int, input().split()) + three, five = [], [] + for _ in range(n): + a, b = map(int, input().split()) + if a == 3: + three.append([a, b]) + else: + five.append([a, b]) + + print(solve(n, m, sorted(three, reverse=True), sorted(five, reverse=True))) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() From d7557d986b9816360197472e8b941a1fa9ea9331 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 5 Dec 2023 16:09:57 +0900 Subject: [PATCH 31/50] move dir baekjoon/17305.py --- 17305.py => baekjoon/17305.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 17305.py => baekjoon/17305.py (100%) diff --git a/17305.py b/baekjoon/17305.py similarity index 100% rename from 17305.py rename to baekjoon/17305.py From d6cbfed79db3476f95f971f2c2e4dee2c86bc57c Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 5 Dec 2023 16:10:48 +0900 Subject: [PATCH 32/50] move to ddingmin/baekjoon/17305.py --- {baekjoon => ddingmin/baekjoon}/17305.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {baekjoon => ddingmin/baekjoon}/17305.py (100%) diff --git a/baekjoon/17305.py b/ddingmin/baekjoon/17305.py similarity index 100% rename from baekjoon/17305.py rename to ddingmin/baekjoon/17305.py From 157fc54fa800966cf9273c92e1982d7c39f86f55 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Wed, 6 Dec 2023 19:43:39 +0900 Subject: [PATCH 33/50] =?UTF-8?q?[BOJ]=2028325:=20=ED=98=B8=EC=88=AB?= =?UTF-8?q?=EA=B0=80=EC=9D=98=20=EA=B0=9C=EB=AF=B8=EA=B5=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/28325.py | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 ddingmin/baekjoon/28325.py diff --git a/ddingmin/baekjoon/28325.py b/ddingmin/baekjoon/28325.py new file mode 100644 index 0000000..15ac7ab --- /dev/null +++ b/ddingmin/baekjoon/28325.py @@ -0,0 +1,41 @@ +import sys + + +def solve(n, arr): + ans = 0 + + # 첫 번째 큰 집을 먹은 경우 + dp = [[0] * n for _ in range(2)] + dp[0][0] = 1 + for i in range(1, n - 1): + # 큰집 먹기 + dp[0][i] = dp[1][i - 1] + 1 + # 작은집 먹기 + dp[1][i] = max(dp[0][i - 1], dp[1][i - 1]) + arr[i] + # 마지막 큰 집은 못먹음 + ans = max(dp[0][n - 2], dp[1][n - 2]) + arr[n - 1] + + # 첫 번째 작은 집을 먹은 경우 + dp = [[0] * n for _ in range(3)] + dp[1][0] = arr[0] + for i in range(1, n): + # 큰집 먹기 + dp[0][i] = dp[1][i - 1] + 1 + # 작은집 먹기 + dp[1][i] = max(dp[0][i - 1], dp[1][i - 1]) + arr[i] + + ans = max(ans, dp[0][n - 1], dp[1][n - 1]) + + return ans + + +def main(): + n = int(input()) + arr = list(map(int, input().split())) + print(solve(n, arr)) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() From 8386536423dba4b7851e01703674e26465c73d06 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Thu, 7 Dec 2023 13:27:27 +0900 Subject: [PATCH 34/50] =?UTF-8?q?[BOJ]=209489:=20=EC=82=AC=EC=B4=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/9489.py | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 ddingmin/baekjoon/9489.py diff --git a/ddingmin/baekjoon/9489.py b/ddingmin/baekjoon/9489.py new file mode 100644 index 0000000..fce1035 --- /dev/null +++ b/ddingmin/baekjoon/9489.py @@ -0,0 +1,57 @@ +import sys + +MAX_MODE = 1001 + + +def node_number_generater(nodes, node): + if node not in nodes: + nodes[node] = len(nodes) + 1 + return nodes[node] + + +def solve(n, k, arr): + adj = [[] for _ in range(MAX_MODE)] + radj = [[] for _ in range(MAX_MODE)] + parents = [] + parent_idx = -1 + nodes = {} + + # 인접 리스트 구현 + root_node = node_number_generater(nodes, arr[0]) + parents.append(node_number_generater(nodes, arr[0])) + for i in range(1, n): + cur = node_number_generater(nodes, arr[i]) + if arr[i] - 1 > arr[i - 1]: + parent_idx += 1 + + adj[parents[parent_idx]].append(cur) + radj[cur].append(parents[parent_idx]) + parents.append(cur) + + # 찾기 + k = node_number_generater(nodes, k) + if len(radj[k]) != 1: return 0 + father_node = radj[k][0] + if len(radj[father_node]) != 1: return 0 + grandfather_node = radj[father_node][0] + + ans = 0 + for f_node in adj[grandfather_node]: + if f_node == father_node: continue + ans += len(adj[f_node]) + + return ans + + +def main(): + while 1: + n, k = map(int, input().split()) + if (n, k) == (0, 0): break + arr = list(map(int, input().split())) + print(solve(n, k, arr)) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() From 3d6778ef3e0629095ebcc225c6acd41b36134c41 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Fri, 8 Dec 2023 18:04:09 +0900 Subject: [PATCH 35/50] =?UTF-8?q?[BOJ]=2018430:=20=EB=AC=B4=EA=B8=B0=20?= =?UTF-8?q?=EA=B3=B5=ED=95=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/18430.py | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 ddingmin/baekjoon/18430.py diff --git a/ddingmin/baekjoon/18430.py b/ddingmin/baekjoon/18430.py new file mode 100644 index 0000000..c46d760 --- /dev/null +++ b/ddingmin/baekjoon/18430.py @@ -0,0 +1,43 @@ +import sys + +dir = [[-1, -1], [-1, 1], [1, -1], [1, 1]] + + +def solve(n, m, arr): + visit = [[0] * m for _ in range(n)] + visited = {} + + def bt(power): + next_power = power + for i in range(n): + for j in range(m): + if visit[i][j]: continue + for dx, dy in dir: + x1, y1 = i + dx, j + x2, y2 = i, j + dy + if not (0 <= x1 < n and 0 <= y1 < m and 0 <= x2 < n and 0 <= y2 < m): + continue + if visit[x1][y1] or visit[x2][y2]: continue + visit[x1][y1], visit[x2][y2], visit[i][j] = 1, 1, 2 + if str(visit) in visited: + visit[x1][y1], visit[x2][y2], visit[i][j] = 0, 0, 0 + continue + visited[str(visit)] = 1 + cur_power = arr[x1][y1] + arr[x2][y2] + arr[i][j] * 2 + next_power = max(next_power, bt(power + cur_power)) + visit[x1][y1], visit[x2][y2], visit[i][j] = 0, 0, 0 + return next_power + + return bt(0) + + +def main(): + n, m = map(int, input().split()) + arr = [list(map(int, input().split())) for _ in range(n)] + print(solve(n, m, arr)) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() From 3c984c1eea2d37fc41c0e8556de8c2441497444b Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sun, 10 Dec 2023 16:53:49 +0900 Subject: [PATCH 36/50] =?UTF-8?q?[BOJ]=209077:=20=EC=A7=80=EB=A2=B0?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/9077.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 ddingmin/baekjoon/9077.py diff --git a/ddingmin/baekjoon/9077.py b/ddingmin/baekjoon/9077.py new file mode 100644 index 0000000..aef6000 --- /dev/null +++ b/ddingmin/baekjoon/9077.py @@ -0,0 +1,31 @@ +import sys + +MAX_RANGE = 10001 + + +def main(): + t = int(input()) + arr = [[0] * MAX_RANGE for _ in range(MAX_RANGE)] + + for _ in range(t): + n = int(input()) + ans = 0 + for _ in range(n): + x, y = map(int, input().split()) + for i in range(x, min(x + 11, MAX_RANGE)): + for j in range(y, min(y + 11, MAX_RANGE)): + arr[i][j] += 1 + ans = max(ans, arr[i][j]) + + for i in range(MAX_RANGE): + for j in range(MAX_RANGE): + arr[i][j] = 0 + + print(ans) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() +9077 From 34f87ed25f01034703d88eba55f200b2cd91ea65 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 12 Dec 2023 01:33:59 +0900 Subject: [PATCH 37/50] =?UTF-8?q?[BOJ]=2014718:=20=EC=9A=A9=EA=B0=90?= =?UTF-8?q?=ED=95=9C=20=EC=9A=A9=EC=82=AC=20=EC=A7=84=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/14718.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ddingmin/baekjoon/14718.py diff --git a/ddingmin/baekjoon/14718.py b/ddingmin/baekjoon/14718.py new file mode 100644 index 0000000..9a1f0e4 --- /dev/null +++ b/ddingmin/baekjoon/14718.py @@ -0,0 +1,29 @@ +import sys + + +def solve(n, need_count, arr): + ans = float('inf') + + for i in range(n): + for j in range(n): + for k in range(n): + can_win_count = 0 + for idx in range(n): + if arr[i][0] >= arr[idx][0] and arr[j][1] >= arr[idx][1] and arr[k][2] >= arr[idx][2]: + can_win_count += 1 + if can_win_count >= need_count: + ans = min(ans, arr[i][0] + arr[j][1] + arr[k][2]) + + return ans + + +def main(): + n, k = map(int, input().split()) + arr = [list(map(int, input().split())) for _ in range(n)] + print(solve(n, k, sorted(arr))) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() From e3063cc646b455a4c9755a399e0b1f0ee5be2262 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 12 Dec 2023 15:29:40 +0900 Subject: [PATCH 38/50] =?UTF-8?q?[BOJ]=2012908:=20=09=ED=85=94=EB=A0=88?= =?UTF-8?q?=ED=8F=AC=ED=8A=B8=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/12908.py | 76 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 ddingmin/baekjoon/12908.py diff --git a/ddingmin/baekjoon/12908.py b/ddingmin/baekjoon/12908.py new file mode 100644 index 0000000..db5c990 --- /dev/null +++ b/ddingmin/baekjoon/12908.py @@ -0,0 +1,76 @@ +import sys +import heapq + + +def get_node(nodes, cur): + if cur not in nodes: + nodes[cur] = len(nodes) + 1 + return nodes[cur] + + +def calc_distance(si, sj, ei, ej): + return abs(si - ei) + abs(sj - ej) + + +def solve(adj, start, end): + # 다익스트라 돌려서 구하기 + costs = [float('inf')] * 10 + costs[start] = 0 + + hq = [] + heapq.heappush(hq, [0, start]) + + while hq: + cur_cost, cur_node = heapq.heappop(hq) + # print(f"costs[cur_node]: {costs[cur_node]}, cur_cost: {cur_cost}, cur_node: {cur_node}") + if costs[cur_node] != cur_cost: continue + for next_node, next_cost in adj[cur_node]: + if cur_cost + next_cost < costs[next_node]: + costs[next_node] = cur_cost + next_cost + heapq.heappush(hq, [costs[next_node], next_node]) + + return costs[end] + + +def main(): + sx, sy = map(int, input().split()) + ex, ey = map(int, input().split()) + teleports = [list(map(int, input().split())) for _ in range(3)] + nodes = {} + adj = [[] * 10 for _ in range(10)] + start, end = get_node(nodes, str([sx, sy])), get_node(nodes, str([ex, ey])) + for i in range(3): + tsx, tsy, tex, tey = teleports[i] + + # tp 이동시간, 직접 이동 시간 중 더 짧은 시간 + t_node_start, t_node_end = get_node(nodes, str([tsx, tsy])), get_node(nodes, str([tex, tey])) + adj[t_node_start].append([t_node_end, min(10, calc_distance(tsx, tsy, tex, tey))]) + adj[t_node_end].append([t_node_start, min(10, calc_distance(tsx, tsy, tex, tey))]) + + # 시작점 -> tps, tpe + adj[start].append([t_node_start, calc_distance(sx, sy, tsx, tsy)]) + adj[start].append([t_node_end, calc_distance(sx, sy, tex, tey)]) + + # tps -> 도착점 + adj[t_node_end].append([end, calc_distance(tex, tey, ex, ey)]) + adj[t_node_start].append([end, calc_distance(tsx, tsy, ex, ey)]) + + for j in range(3): + if i == j: continue + ttsx, ttsy, ttex, ttey = teleports[j] + tt_node_start, tt_node_end = get_node(nodes, str([ttsx, ttsy])), get_node(nodes, str([ttex, ttey])) + + adj[t_node_end].append([tt_node_start, calc_distance(tex, tey, ttsx, ttsy)]) + adj[t_node_end].append([tt_node_end, calc_distance(tex, tey, ttex, ttey)]) + adj[t_node_start].append([tt_node_start, calc_distance(tsx, tsy, ttsx, ttsy)]) + adj[t_node_start].append([tt_node_end, calc_distance(tsx, tsy, ttex, ttey)]) + + # 바로 가는 거리랑 비교 + distance = min(calc_distance(sx, sy, ex, ey), solve(adj, start, end)) + print(distance) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() From a7e93e1171885a6cb54d3b062bc950718a3882c0 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Thu, 14 Dec 2023 03:21:18 +0900 Subject: [PATCH 39/50] =?UTF-8?q?[BOJ]=2028017:=20=EA=B2=8C=EC=9E=84?= =?UTF-8?q?=EC=9D=84=20=ED=81=B4=EB=A6=AC=EC=96=B4=ED=95=98=EC=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/28017.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ddingmin/baekjoon/28017.py diff --git a/ddingmin/baekjoon/28017.py b/ddingmin/baekjoon/28017.py new file mode 100644 index 0000000..6a5aba2 --- /dev/null +++ b/ddingmin/baekjoon/28017.py @@ -0,0 +1,29 @@ +import sys + + +def solve(n, m, arr): + dp = [[float('inf')] * m for _ in range(n)] + + # 초기값 세팅 + for j in range(m): + dp[0][j] = arr[0][j] + + # 점화식: min(이전값들 중 최소 + 현재 무기) [같은 무기는 제외] + for i in range(1, n): + for j in range(m): + for k in range(m): + if j == k: continue + dp[i][j] = min(dp[i][j], dp[i - 1][k] + arr[i][j]) + + return min(dp[-1]) + + +def main(): + n, m = map(int, input().split()) + arr = [list(map(int, input().split())) for _ in range(n)] + print(solve(n, m, arr)) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From 4e26d515affeeaa88bdc1a177e2d3f3f2e2d4dad Mon Sep 17 00:00:00 2001 From: ddingmin Date: Thu, 14 Dec 2023 13:28:43 +0900 Subject: [PATCH 40/50] =?UTF-8?q?[BOJ]=2012764:=20=EC=8B=B8=EC=A7=80?= =?UTF-8?q?=EB=B0=A9=EC=97=90=20=EA=B0=84=20=EC=A4=80=ED=95=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 12764.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 12764.py diff --git a/12764.py b/12764.py new file mode 100644 index 0000000..3aa651b --- /dev/null +++ b/12764.py @@ -0,0 +1,38 @@ +import sys +import heapq + + +def solve(n, arr): + ans = [] + hq = [] + empty = [] + + for start, end in arr: + # 이용자 정리 + while hq and hq[0][0] <= start: + t, seat = heapq.heappop(hq) + heapq.heappush(empty, seat) + + # 남아있는 자리가 없는 경우 + if len(empty) == 0: + ans.append(1) + heapq.heappush(hq, [end, len(ans) - 1]) + else: + seat = heapq.heappop(empty) + heapq.heappush(hq, [end, seat]) + ans[seat] += 1 + + return ans + + +def main(): + n = int(input()) + arr = [list(map(int, input().split())) for _ in range(n)] + ans = solve(n, sorted(arr)) + print(len(ans)) + print(*ans) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From dc26cf04d66628e43a1ee011e1f3abcfc19e6ed0 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Thu, 14 Dec 2023 13:30:25 +0900 Subject: [PATCH 41/50] =?UTF-8?q?[BOJ]=2012764:=20=EC=8B=B8=EC=A7=80?= =?UTF-8?q?=EB=B0=A9=EC=97=90=20=EA=B0=84=20=EC=A4=80=ED=95=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/12764.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 ddingmin/baekjoon/12764.py diff --git a/ddingmin/baekjoon/12764.py b/ddingmin/baekjoon/12764.py new file mode 100644 index 0000000..3aa651b --- /dev/null +++ b/ddingmin/baekjoon/12764.py @@ -0,0 +1,38 @@ +import sys +import heapq + + +def solve(n, arr): + ans = [] + hq = [] + empty = [] + + for start, end in arr: + # 이용자 정리 + while hq and hq[0][0] <= start: + t, seat = heapq.heappop(hq) + heapq.heappush(empty, seat) + + # 남아있는 자리가 없는 경우 + if len(empty) == 0: + ans.append(1) + heapq.heappush(hq, [end, len(ans) - 1]) + else: + seat = heapq.heappop(empty) + heapq.heappush(hq, [end, seat]) + ans[seat] += 1 + + return ans + + +def main(): + n = int(input()) + arr = [list(map(int, input().split())) for _ in range(n)] + ans = solve(n, sorted(arr)) + print(len(ans)) + print(*ans) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From d8e6ef417bccc5c2751021a59ee8abcaa8e0d5c1 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Thu, 14 Dec 2023 13:33:05 +0900 Subject: [PATCH 42/50] =?UTF-8?q?[BOJ]=2012764:=20=EC=8B=B8=EC=A7=80?= =?UTF-8?q?=EB=B0=A9=EC=97=90=20=EA=B0=84=20=EC=A4=80=ED=95=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 12764.py | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 12764.py diff --git a/12764.py b/12764.py deleted file mode 100644 index 3aa651b..0000000 --- a/12764.py +++ /dev/null @@ -1,38 +0,0 @@ -import sys -import heapq - - -def solve(n, arr): - ans = [] - hq = [] - empty = [] - - for start, end in arr: - # 이용자 정리 - while hq and hq[0][0] <= start: - t, seat = heapq.heappop(hq) - heapq.heappush(empty, seat) - - # 남아있는 자리가 없는 경우 - if len(empty) == 0: - ans.append(1) - heapq.heappush(hq, [end, len(ans) - 1]) - else: - seat = heapq.heappop(empty) - heapq.heappush(hq, [end, seat]) - ans[seat] += 1 - - return ans - - -def main(): - n = int(input()) - arr = [list(map(int, input().split())) for _ in range(n)] - ans = solve(n, sorted(arr)) - print(len(ans)) - print(*ans) - - -if __name__ == '__main__': - input = sys.stdin.readline - main() From 306e97840b565d76676311c9190e78f0e84ad80c Mon Sep 17 00:00:00 2001 From: ddingmin Date: Fri, 15 Dec 2023 18:47:57 +0900 Subject: [PATCH 43/50] =?UTF-8?q?[BOJ]=2016569:=20=ED=99=94=EC=82=B0?= =?UTF-8?q?=EC=87=84=EC=84=A4=EB=A5=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/16569.py | 88 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 ddingmin/baekjoon/16569.py diff --git a/ddingmin/baekjoon/16569.py b/ddingmin/baekjoon/16569.py new file mode 100644 index 0000000..c3db8f7 --- /dev/null +++ b/ddingmin/baekjoon/16569.py @@ -0,0 +1,88 @@ +import sys +import heapq +from collections import deque + +dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1] + + +def flow_fire(fire_q, fire, n, m): + # 화산이 흐른다. + for idx in range(len(fire_q)): + i, j = fire_q[idx] + for k in range(4): + x, y = i + dx[k], j + dy[k] + if not (0 <= x < n and 0 <= y < m): continue + if not fire[x][y]: + fire[x][y] = 1 + fire_q.append([x, y]) + + +def hole_start(hole, now, q, fire): + # 시간이 다 된 화산 분출구를 q에 넣는다. + while hole and hole[0][0] < now: + t, x, y = heapq.heappop(hole) + q.append([x, y]) + fire[x][y] = 1 + + + +def solve(n, m, v, sx, sy, highs, fire, hole, visit): + ans = (highs[sx][sy], 0) + visit[sx][sy] = 1 + now = 0 + fire_q = deque() + people_q = deque() + people_q.append([sx, sy]) + hole_start(hole, now, fire_q, fire) + while people_q: + now += 1 + hole_start(hole, now, fire_q, fire) + flow_fire(fire_q, fire, n, m) + + # print(f"{now}시간의 화산 상황") + # for pp in fire: + # print(*pp) + # print() + # + # print(f"{now}시간의 이동 상황") + # for pp in visit: + # print(*pp) + # print() + # + # print(f"정답: {ans}") + # print(f"이동 큐: {people_q}") + + for _ in range(len(people_q)): + cx, cy = people_q.popleft() + for k in range(4): + nx, ny = cx + dx[k], cy + dy[k] + if not (0 <= nx < n and 0 <= ny < m): continue + if fire[nx][ny] or visit[nx][ny]: continue + visit[nx][ny] = now + people_q.append([nx, ny]) + # 정답 갱신 + if ans[0] < highs[nx][ny]: + ans = (highs[nx][ny], now) + + return ans + + +def main(): + n, m, v = map(int, input().split()) + sx, sy = map(int, input().split()) + highs = [list(map(int, input().split())) for _ in range(n)] + visit = [[0] * m for _ in range(n)] + fire = [[0] * m for _ in range(n)] # 이동할 수 없는 지역 + hole = [] + for _ in range(v): + x, y, t = map(int, input().split()) + y, x = y - 1, x - 1 + visit[x][y] = 1 + heapq.heappush(hole, [t, x, y]) + print(*solve(n, m, v, sx - 1, sy - 1, highs, fire, hole, visit)) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() From 472d696478bc569e097f83dd99134b20c4efcd9b Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sat, 16 Dec 2023 09:15:54 +0900 Subject: [PATCH 44/50] =?UTF-8?q?[BOJ]=205546:=20=ED=8C=8C=EC=8A=A4?= =?UTF-8?q?=ED=83=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/5546.py | 52 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 ddingmin/baekjoon/5546.py diff --git a/ddingmin/baekjoon/5546.py b/ddingmin/baekjoon/5546.py new file mode 100644 index 0000000..40ed048 --- /dev/null +++ b/ddingmin/baekjoon/5546.py @@ -0,0 +1,52 @@ +import sys + +MOD = 10000 + + +def solve(n, m, pastas): + dp = [[0] * 3 for _ in range(n + 1)] + + # 1일차 세팅 + if pastas[1] is None: + dp[1] = [1, 1, 1] + else: + dp[1][pastas[1]] = 1 + # 2일차 세팅 + if pastas[2] is not None: + dp[2][pastas[2]] = sum(dp[1]) + else: + for t in range(3): + dp[2][t] = sum(dp[1]) + + for day in range(3, n + 1): + for t in range(3): + if dp[day - 1][t] == 0: + dp[day][t] = sum(dp[day - 1]) % MOD + else: + dp[day][t] = ( + sum(dp[day - 1]) - dp[day - 1][t] + + sum(dp[day - 2]) - dp[day - 2][t] + ) % MOD + + if pastas[day] is not None: + dp[day][(pastas[day] + 1) % 3] = 0 + dp[day][(pastas[day] + 2) % 3] = 0 + + ans = sum(dp[n]) % MOD + return ans + + +def main(): + n, m = map(int, input().split()) + pastas = [None] * (n + 1) + + for _ in range(m): + day, p = map(int, input().split()) + pastas[day] = p - 1 + + print(solve(n, m, pastas)) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From 97b9763521d5cd166106ce5a9a79d2368ab5c496 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 19 Dec 2023 02:54:44 +0900 Subject: [PATCH 45/50] =?UTF-8?q?[BOJ]=2023740:=20=EB=B2=84=EC=8A=A4=20?= =?UTF-8?q?=EB=85=B8=EC=84=A0=20=EA=B0=9C=ED=8E=B8=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/23740.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 ddingmin/baekjoon/23740.py diff --git a/ddingmin/baekjoon/23740.py b/ddingmin/baekjoon/23740.py new file mode 100644 index 0000000..034d281 --- /dev/null +++ b/ddingmin/baekjoon/23740.py @@ -0,0 +1,32 @@ +import sys + + +def solve(n, cmds): + answer = [] + + # 초기 데이터 세팅 + start, end, cost = cmds[0] + + for s, e, c in cmds[1:]: + if s <= end: # 구간 연결 + end = max(end, e) + cost = min(cost, c) + else: # 경로 추가 + answer.append([start, end, cost]) + start, end, cost = s, e, c + # 마지막 경로 추가 + answer.append([start, end, cost]) + print(len(answer)) + for pp in answer: + print(*pp) + + +def main(): + n = int(input()) + arr = [list(map(int, input().split())) for _ in range(n)] + solve(n, sorted(arr)) + + +if __name__ == '__main__': + input = sys.stdin.readline + main() From 8ecfedcbd21070169753d56da2404e6bfc5e0179 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Tue, 19 Dec 2023 23:44:09 +0900 Subject: [PATCH 46/50] =?UTF-8?q?[BOJ]=2020440:=20=F0=9F=8E=B5=EB=8B=88?= =?UTF-8?q?=EA=B0=80=20=EC=8B=AB=EC=96=B4=20=EC=8B=AB=EC=96=B4=20=EB=84=88?= =?UTF-8?q?=EB=AC=B4=20=EC=8B=AB=EC=96=B4=20=EC=8B=AB=EC=96=B4=20=EC=98=A4?= =?UTF-8?q?=EC=A7=80=20=EB=A7=88=20=EB=82=B4=EA=B2=8C=20=EC=B0=9D=EC=A9=8D?= =?UTF-8?q?=EB=8C=80=EC=A7=80=EB=A7=88=F0=9F=8E=B5=20-=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/20440.py | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 ddingmin/baekjoon/20440.py diff --git a/ddingmin/baekjoon/20440.py b/ddingmin/baekjoon/20440.py new file mode 100644 index 0000000..6237de6 --- /dev/null +++ b/ddingmin/baekjoon/20440.py @@ -0,0 +1,44 @@ +import sys + + +def solve(n, cmds): + dic = {} + + for s, e in cmds: + if s not in dic: + dic[s] = 0 + if e not in dic: + dic[e] = 0 + dic[s] += 1 + dic[e] -= 1 + + ans = 0 + ans_range = [0, 0] + prev = 0 + need_range = False + + for k in sorted(dic.keys()): + prev += dic[k] + if prev > ans: + ans = prev + ans_range[0] = k + need_range = True + elif need_range and dic[k] < 0: + need_range = False + ans_range[1] = k + + print(ans) + print(*ans_range) + return ans + + +def main(): + n = int(input()) + arr = [list(map(int, input().split())) for _ in range(n)] + solve(n, arr) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() From 340a046ada14d07236ebf0c8d748dfbaeacbc715 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Fri, 22 Dec 2023 00:43:21 +0900 Subject: [PATCH 47/50] =?UTF-8?q?[BOJ]=2017299:=20=EC=98=A4=EB=93=B1?= =?UTF-8?q?=ED=81=B0=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/17299.py | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 ddingmin/baekjoon/17299.py diff --git a/ddingmin/baekjoon/17299.py b/ddingmin/baekjoon/17299.py new file mode 100644 index 0000000..4019663 --- /dev/null +++ b/ddingmin/baekjoon/17299.py @@ -0,0 +1,59 @@ +import sys + +MAX_VALUE = 1_000_000 + + +def solve(n, arr): + # 갯수 배열로 변환 + cnt = {} + + for i in arr: + if i not in cnt: + cnt[i] = 0 + cnt[i] += 1 + + cnt_arr = [] + for i in range(n): + cnt_arr.append([cnt[arr[i]], i]) + + ans = [0] * n + + stack = [] + + # 뒤에서부터 스택으로 해결 + for new, idx in cnt_arr[::-1]: + # 새로 들어오는 값이 더 큰 경우 + if stack and stack[-1][0] < new: + # 값이 더 큰 값이 없을 때 까지 pop + while stack and stack[-1][0] < new: + stack.pop() + + # 스택이 없는 경우는 그냥 집어넣고 ans = -1 + if not stack: + ans[idx] = -1 + stack.append([new, idx]) + # 스택의 가장 상단값보다 새로 들어오는 값이 작은 경우 ans는 최상단의 실제 값 + elif stack and stack[-1][0] > new: + prev, prev_idx = stack[-1] + ans[idx] = arr[prev_idx] + stack.append([new, idx]) + # 스택의 가장 상단값과 새로 들어오는 값이 같은 경우 ans는 최상단의 정답 값 + elif stack and stack[-1][0] == new: + prev, prev_idx = stack[-1] + ans[idx] = ans[prev_idx] + stack.append([new, idx]) + + print(*ans) + return 0 + + +def main(): + n = int(input()) + arr = list(map(int, input().split())) + solve(n, arr) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() From 1c5f83faeec331e5f33fef52552a1f347f1c0b47 Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sat, 23 Dec 2023 00:21:42 +0900 Subject: [PATCH 48/50] =?UTF-8?q?[BOJ]=2012834:=20=EC=A3=BC=EA=B0=84=20?= =?UTF-8?q?=EB=AF=B8=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/12834.py | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 ddingmin/baekjoon/12834.py diff --git a/ddingmin/baekjoon/12834.py b/ddingmin/baekjoon/12834.py new file mode 100644 index 0000000..6fccd8c --- /dev/null +++ b/ddingmin/baekjoon/12834.py @@ -0,0 +1,45 @@ +import sys +import heapq + + +def solve(n, v, e, KIST, CRFOOD, house, adj): + return dijk(KIST, adj, v, house) + dijk(CRFOOD, adj, v, house) + + +def dijk(start, adj, v, house): + visit = [float('inf')] * (v + 1) + visit[start] = 0 + hq = [[0, start]] + while hq: + cur_cost, cur_node = heapq.heappop(hq) + if cur_cost != visit[cur_node]: continue + for next_node, next_cost in adj[cur_node]: + value = cur_cost + next_cost + if value < visit[next_node]: + visit[next_node] = value + heapq.heappush(hq, [value, next_node]) + distance = 0 + for i in house: + distance += visit[i] if visit[i] != float('inf') else -1 + + return distance + + +def main(): + n, v, e = map(int, input().split()) + KIST, CRFOOD = map(int, input().split()) + house = list(map(int, input().split())) + + adj = [[] for _ in range(v + 1)] + for _ in range(e): + a, b, c = map(int, input().split()) + adj[a].append([b, c]) + adj[b].append([a, c]) + + print(solve(n, v, e, KIST, CRFOOD, house, adj)) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() From 4cee3563758d21023a704142b7a37ed3b7c0f98b Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sat, 23 Dec 2023 10:44:57 +0900 Subject: [PATCH 49/50] =?UTF-8?q?[BOJ]=202140:=20=EC=A7=80=EB=A2=B0?= =?UTF-8?q?=EC=B0=BE=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/2140.py | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 ddingmin/baekjoon/2140.py diff --git a/ddingmin/baekjoon/2140.py b/ddingmin/baekjoon/2140.py new file mode 100644 index 0000000..d0ade58 --- /dev/null +++ b/ddingmin/baekjoon/2140.py @@ -0,0 +1,70 @@ +import sys +import heapq + +dx, dy = [-1, -1, -1, 0, 0, 1, 1, 1], [-1, 0, 1, -1, 1, -1, 0, 1] + + +def solve(n, arr): + if n < 3: + return 0 + elif n == 3 and int(arr[0][0]) == 1: + return 1 + + ans = 0 + + for i in range(1, n - 2): + cnt = 0 + if arr[1][i - 1] == '*': cnt += 1 + if arr[1][i] == '*': cnt += 1 + + if int(arr[0][i - 1]) > cnt: + ans += 1 + arr[1][i] = '*' + arr[0][i + 1] = int(arr[0][i + 1]) - 1 + + for i in range(1, n - 2): + cnt = 0 + if arr[i - 1][n - 2] == '*': cnt += 1 + if arr[i][n - 2] == '*': cnt += 1 + + if int(arr[i - 1][n - 1]) > cnt: + ans += 1 + arr[i][n - 2] = '*' + arr[i + 1][n - 1] = int(arr[i + 1][n - 1]) - 1 + + for i in range(n - 2, 1, -1): + cnt = 0 + if arr[n - 2][i + 1] == '*': cnt += 1 + if arr[n - 2][i] == '*': cnt += 1 + + if int(arr[n - 1][i + 1]) > cnt: + ans += 1 + arr[n - 2][i] = '*' + arr[n - 1][i - 1] = int(arr[n - 1][i - 1]) - 1 + + for i in range(n - 2, 1, -1): + cnt = 0 + if arr[i + 1][1] == '*': cnt += 1 + if arr[i][1] == '*': cnt += 1 + + if int(arr[i + 1][0]) > cnt: + ans += 1 + arr[i][1] = '*' + arr[i - 1][0] = int(arr[i - 1][0]) - 1 + + if n > 4: + ans += (n - 4) ** 2 + + return ans + + +def main(): + n = int(input()) + arr = [list(input().strip()) for _ in range(n)] + print(solve(n, arr)) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main() From db3a4c9973a66880a3cf902b26944b770cb7fd7b Mon Sep 17 00:00:00 2001 From: ddingmin Date: Sun, 24 Dec 2023 22:58:55 +0900 Subject: [PATCH 50/50] =?UTF-8?q?[BOJ]=205502:=20=ED=8C=B0=EB=A6=B0?= =?UTF-8?q?=EB=93=9C=EB=A1=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddingmin/baekjoon/5502.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ddingmin/baekjoon/5502.py diff --git a/ddingmin/baekjoon/5502.py b/ddingmin/baekjoon/5502.py new file mode 100644 index 0000000..ca88ad0 --- /dev/null +++ b/ddingmin/baekjoon/5502.py @@ -0,0 +1,27 @@ +import sys + + +def solve(n, word, reverse_word): + lcs = [[0] * (n + 1) for _ in range(n + 1)] + + for i in range(1, n + 1): + for j in range(1, n + 1): + if word[i] == reverse_word[j]: + lcs[i][j] = lcs[i - 1][j - 1] + 1 + else: + lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]) + + return n - lcs[-1][-1] + + +def main(): + n = int(input()) + word = input().strip() + reverse_word = word[::-1] + print(solve(n, '0' + word, '0' + reverse_word)) + + +if __name__ == '__main__': + # sys.setrecursionlimit(10 ** 6) + input = sys.stdin.readline + main()