From ace0bc98f232252b6233e12a0cca01432544f6f4 Mon Sep 17 00:00:00 2001 From: timepresent95 Date: Sun, 1 Sep 2024 12:34:08 +0900 Subject: [PATCH 1/7] #43 11660.py --- sean/class4-2/11660.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 sean/class4-2/11660.py diff --git a/sean/class4-2/11660.py b/sean/class4-2/11660.py new file mode 100644 index 0000000..f82bf71 --- /dev/null +++ b/sean/class4-2/11660.py @@ -0,0 +1,23 @@ +import sys + +# sys.stdin = open("input.txt", "r") + +n, m = map(int, input().split()) + +arr = [list(map(int, input().split())) for _ in range(n)] + +sum_arr = [[0] * n for _ in range(n)] + +for i in range(0, n): + for j in range(n): + prev_row = sum_arr[i - 1][j] if (i != 0) else 0 + prev_col = sum_arr[i][j - 1] if (j != 0) else 0 + prev_sum = sum_arr[i - 1][j - 1] if (i != 0 and j != 0) else 0 + sum_arr[i][j] = arr[i][j] + prev_row + prev_col - prev_sum + +for _ in range(m): + x1, y1, x2, y2 = map(lambda x: int(x) - 1, input().split()) + prev_row = sum_arr[x1 - 1][y2] if (x1 != 0) else 0 + prev_col = sum_arr[x2][y1 - 1] if (y1 != 0) else 0 + prev_sum = sum_arr[x1 - 1][y1 - 1] if (x1 != 0 and y1 != 0) else 0 + print(sum_arr[x2][y2] - prev_row - prev_col + prev_sum) From 0886d9816ba3c33afa60ca9d78e82c8a3440f21a Mon Sep 17 00:00:00 2001 From: timepresent95 Date: Sun, 1 Sep 2024 16:02:02 +0900 Subject: [PATCH 2/7] #43 9251.py --- sean/class4-2/9251.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 sean/class4-2/9251.py diff --git a/sean/class4-2/9251.py b/sean/class4-2/9251.py new file mode 100644 index 0000000..19802d2 --- /dev/null +++ b/sean/class4-2/9251.py @@ -0,0 +1,22 @@ +import sys + +# sys.stdin = open("input.txt", "r") + +sequence = [input(), input()] + +sequence_table = [[0] * (len(sequence[1]) + 1) for _ in range(len(sequence[0]) + 1)] + + +answer = 0 +for i in range(1, len(sequence[0]) + 1): + for j in range(1, len(sequence[1]) + 1): + if sequence[0][i - 1] == sequence[1][j - 1]: + prev = sequence_table[i - 1][j - 1] if (i != 0 and j != 0) else 0 + sequence_table[i][j] = prev + 1 + else: + prev_row = sequence_table[i - 1][j] if (i != 0) else 0 + prev_col = sequence_table[i][j - 1] if (j != 0) else 0 + sequence_table[i][j] = max(prev_row, prev_col) + answer = max(answer, sequence_table[i][j]) + +print(answer) \ No newline at end of file From 67b83d987ab9f252d030948d4c813f01239cbeb6 Mon Sep 17 00:00:00 2001 From: timepresent95 Date: Sun, 1 Sep 2024 17:18:28 +0900 Subject: [PATCH 3/7] #43 12865.py --- sean/class4-2/12865.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 sean/class4-2/12865.py diff --git a/sean/class4-2/12865.py b/sean/class4-2/12865.py new file mode 100644 index 0000000..c368682 --- /dev/null +++ b/sean/class4-2/12865.py @@ -0,0 +1,22 @@ +import sys + +# sys.stdin = open("input.txt", "r") + +n, k = map(int, input().split()) + +items = sorted( + [list(map(int, input().split())) for _ in range(n)], + key=lambda x: x[1] / x[0], + reverse=True, +) + +dp = [0] * (k + 1) + +answer = 0 +for weight, price in items: + for i in range(k, weight - 1, -1): + if i - weight >= 0: + dp[i] = max(dp[i], dp[i - weight] + price) + answer = max(answer, dp[i]) + +print(answer) From 6e046b7bf411360318737e6e92c9aced8bdf926a Mon Sep 17 00:00:00 2001 From: timepresent95 Date: Sun, 1 Sep 2024 17:44:50 +0900 Subject: [PATCH 4/7] #43 13549.py --- sean/class4-2/13549.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 sean/class4-2/13549.py diff --git a/sean/class4-2/13549.py b/sean/class4-2/13549.py new file mode 100644 index 0000000..d81f597 --- /dev/null +++ b/sean/class4-2/13549.py @@ -0,0 +1,27 @@ +from collections import deque +import sys + +# sys.stdin = open("input.txt", "r") + +n, k = map(int, input().split()) + +MAX_POS = 200000 + +dp = [MAX_POS + 1] * (MAX_POS + 1) + +queue = deque([n]) +dp[n] = 0 + +while queue: + x = queue.popleft() + if x != 0 and dp[x - 1] > dp[x] + 1: + dp[x - 1] = dp[x] + 1 + queue.append(x - 1) + if x + 1 <= MAX_POS and dp[x + 1] > dp[x] + 1: + dp[x + 1] = dp[x] + 1 + queue.append(x + 1) + if 2 * x <= MAX_POS and dp[2 * x] > dp[x]: + dp[2 * x] = dp[x] + queue.append(2 * x) + +print(dp[k]) From e18dbebe0e1a02c8cb042a6d3b0eb2a110d5c8f7 Mon Sep 17 00:00:00 2001 From: timepresent95 Date: Mon, 2 Sep 2024 03:07:16 +0900 Subject: [PATCH 5/7] #43 1753.py --- sean/class4-2/1753.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 sean/class4-2/1753.py diff --git a/sean/class4-2/1753.py b/sean/class4-2/1753.py new file mode 100644 index 0000000..7b3ecce --- /dev/null +++ b/sean/class4-2/1753.py @@ -0,0 +1,33 @@ +import heapq +import sys + +# sys.stdin = open("input.txt", "r") + +v, e = map(int, input().split()) +k = int(input()) + + +graph = {i: [] for i in range(1, v + 1)} +for i in range(e): + _u, _v, _w = map(int, input().split()) + graph[_u].append((_v, _w)) + +distance = [float("inf")] * (v + 1) +distance[k] = 0 + +pq = [] +heapq.heappush(pq, (0, k)) + +while pq: + _w, cur = heapq.heappop(pq) + + if _w > distance[cur]: + continue + + for next, weight in graph[cur]: + next_weight = _w + weight + if next_weight < distance[next]: + distance[next] = next_weight + heapq.heappush(pq, (next_weight, next)) + +print("\n".join(map(lambda x: str(x) if x != float("inf") else "INF", distance[1:]))) From 77c47485c1a5e130d8595914935160b20bb331fd Mon Sep 17 00:00:00 2001 From: timepresent95 Date: Mon, 2 Sep 2024 09:30:07 +0900 Subject: [PATCH 6/7] #43 1967.py --- sean/class4-2/1967.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 sean/class4-2/1967.py diff --git a/sean/class4-2/1967.py b/sean/class4-2/1967.py new file mode 100644 index 0000000..cdce496 --- /dev/null +++ b/sean/class4-2/1967.py @@ -0,0 +1,30 @@ +from collections import deque +import sys + +# sys.stdin = open("input.txt", "r") + +n = int(input()) + +tree = [[] for _ in range(n + 1)] + +for _ in range(n - 1): + a, b, c = map(int, input().split()) + tree[a].append((b, c)) + tree[b].append((a, c)) + +answer = 0 +for root in range(1, n + 1): + visited = [-1] * (n + 1) + visited[root] = 0 + + queue = deque([root]) + while queue: + cur = queue.popleft() + + for next, weight in tree[cur]: + if visited[next] == -1: + visited[next] = visited[cur] + weight + queue.append(next) + answer = max(answer, visited[next]) + +print(answer) From 2bbe850c0340e08195f7bccc31730b5e4d00202c Mon Sep 17 00:00:00 2001 From: timepresent95 Date: Mon, 2 Sep 2024 11:34:27 +0900 Subject: [PATCH 7/7] #43 9663.py --- sean/class4-2/9663.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 sean/class4-2/9663.py diff --git a/sean/class4-2/9663.py b/sean/class4-2/9663.py new file mode 100644 index 0000000..1eeb6c7 --- /dev/null +++ b/sean/class4-2/9663.py @@ -0,0 +1,26 @@ +import sys + +# sys.stdin = open("input.txt", "r") + +n = int(input()) + + +def n_queen(row, col, sum, diff): + if row == n: + return 1 + + ret = 0 + for j in range(0, n): + if col[j] or sum[row + j] or diff[row - j + n]: + continue + col[j] = True + sum[row + j] = True + diff[row - j + n] = True + ret = ret + n_queen(row + 1, col, sum, diff) + col[j] = False + sum[row + j] = False + diff[row - j + n] = False + return ret + + +print(n_queen(0, [False] * n, [False] * (2 * n), [False] * (2 * n)))