Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sean/class4-2/11660.py
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 22 additions & 0 deletions sean/class4-2/12865.py
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 27 additions & 0 deletions sean/class4-2/13549.py
Original file line number Diff line number Diff line change
@@ -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])
33 changes: 33 additions & 0 deletions sean/class4-2/1753.py
Original file line number Diff line number Diff line change
@@ -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:])))
30 changes: 30 additions & 0 deletions sean/class4-2/1967.py
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 22 additions & 0 deletions sean/class4-2/9251.py
Original file line number Diff line number Diff line change
@@ -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)
26 changes: 26 additions & 0 deletions sean/class4-2/9663.py
Original file line number Diff line number Diff line change
@@ -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)))