This commit is contained in:
Justin Lin
2026-05-02 15:42:52 +10:00
commit 8582780ac9
38 changed files with 460 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
n, k = map(int, input().split(" "))
arr = [int(x) for x in input().strip().split()]
arr.sort()
pairs = 0
i = 0
while i < n - 1:
if arr[i + 1] - arr[i] <= k:
pairs += 1
i += 2
else:
i += 1
print(pairs)
+2
View File
@@ -0,0 +1,2 @@
6 2
1 3 5 2 4 6
+2
View File
@@ -0,0 +1,2 @@
5 1
1 5 3 2 4
+2
View File
@@ -0,0 +1,2 @@
7 0
1 3 1 2 2 5 4
+7
View File
@@ -0,0 +1,7 @@
5 4
1 5
1 2 3 4 5
1 2 1
1 3 2
4 2 3
5 4 7
+12
View File
@@ -0,0 +1,12 @@
6 9
3 4
2 17 8 6 9 3
1 2 6
1 3 10
1 4 21
2 3 5
2 5 7
3 6 11
4 5 31
4 6 4
5 6 28
+51
View File
@@ -0,0 +1,51 @@
import math
from queue import PriorityQueue
def read_idx(x: str):
return int(x) - 1
def inputs() -> list[str]:
return input().strip(" ").split(" ")
V, E = map(int, inputs())
s, t = map(read_idx, inputs())
vw = list(map(int, inputs())) # vertex weight
graph = [[] for _ in range(V)]
for _ in range(E):
u, v, w = map(read_idx, inputs())
w += 1
graph[u].append((v, w))
graph[v].append((u, w))
newgraph = [[] for _ in range(V)]
for u in range(V):
for v, w in graph[u]:
newgraph[u].append((v, vw[u] + w))
def dijstra(g: list[list[tuple[int, int]]], s: int, t: int) -> float:
dist = [math.inf] * V
dist[s] = 0
q = PriorityQueue()
q.put((0, s))
while not q.empty():
d, u = q.get()
if d != dist[u]:
continue
# print(u, d)
for v, w in g[u]:
if dist[v] > dist[u] + w:
dist[v] = dist[u] + w
q.put((dist[v], v))
return dist[t]
# print(vw, graph)
# print(newgraph)
print(dijstra(newgraph, s, t))
+2
View File
@@ -0,0 +1,2 @@
3
8 6 7
+2
View File
@@ -0,0 +1,2 @@
5
4 4 1 9 2
+2
View File
@@ -0,0 +1,2 @@
1
1000
+14
View File
@@ -0,0 +1,14 @@
n = int(input())
suspects = [int(x) for x in input().strip().split(" ")]
suspects.sort(reverse=True)
if n > 1:
result = 0
sums = [suspects[0]]
for i in range(1, n):
sums.append(sums[-1] + suspects[i])
result = sum(sums[1:])
print(result)
else:
print(suspects[0])
+2
View File
@@ -0,0 +1,2 @@
3
1 2 3
+2
View File
@@ -0,0 +1,2 @@
4
1 2 3 4
+2
View File
@@ -0,0 +1,2 @@
5
10 20 30 40 50
+5
View File
@@ -0,0 +1,5 @@
n = int(input())
input() # ignore the numbers
ans = 2 ** (n - 1) - 2
print(max(ans, 0) % 998244353)
+4
View File
@@ -0,0 +1,4 @@
3 10 10
2 2 20
5 2 0
1 2 12
+3
View File
@@ -0,0 +1,3 @@
2 0 0
1 2 4
2 1 4
+3
View File
@@ -0,0 +1,3 @@
2 0 4
1 1 5
0 3 5
View File
+80
View File
@@ -0,0 +1,80 @@
import math
import sys
sys.setrecursionlimit(100000)
def inputs():
return map(int, input().strip().split(" "))
n, wet_c, dry_c = inputs()
costs = [[(math.inf, -1)] * (dry_c + 1) for _ in range(wet_c + 1)]
costs[0][0] = (0, -1)
bundles = []
for i in range(n):
wet, dry, cost = inputs()
bundles.append((wet, dry, cost))
if wet <= wet_c and dry <= dry_c:
costs[wet][dry] = (cost, i)
def get_cost(w, d) -> float:
if w == 0 and d == 0:
return 0
if w < 0 or d < 0:
return math.inf
if costs[w][d][1] != -1:
print("reusing: ", w, d)
return costs[w][d][0]
print(w, d)
for i in range(len(bundles)):
wet, dry, cost = bundles[i]
new_cost = get_cost(w - wet, d - dry) + cost
if costs[w][d][0] > new_cost:
costs[w][d] = (new_cost, i)
return costs[w][d][0]
def bottom_up() -> float:
for w in range(wet_c + 1):
for d in range(dry_c + 1):
for i in range(len(bundles)):
wet, dry, cost = bundles[i]
if wet > w or dry > d:
continue
new_cost = costs[w - wet][d - dry][0] + cost
if costs[w][d][0] > new_cost:
costs[w][d] = (new_cost, i)
return costs[wet_c][dry_c][0]
def backtrack(w, d) -> list[int]:
counter = [0] * n
b = costs[w][d][1]
while b != -1:
counter[b] += 1
wet, dry, _ = bundles[b]
w -= wet
d -= dry
b = costs[w][d][1]
return counter
# c = get_cost(wet_c, dry_c)
c = bottom_up()
# print(costs)
if math.isinf(c):
print(-1)
else:
print(c)
for c in backtrack(wet_c, dry_c):
print(c)
+25
View File
@@ -0,0 +1,25 @@
a, b = map(int, input().split(" "))
memo = {}
def win(i: int, j: int) -> bool:
# print(f"kirby: {i}, {j}")
if i < j:
return win(j, i)
if j == 1:
return True
if j <= 0:
return False
if (i, j) in memo:
return memo[(i, j)]
memo[(i, j)] = not win(i - 1, j) or not win(i, j - 1)
return memo[(i, j)]
if win(a, b):
print("Kirby")
else:
print("Dedede")
Executable
+28
View File
@@ -0,0 +1,28 @@
#!/bin/bash
echo "0 2" | python q2.py
echo "expected: Dedede"
echo "1 2" | python q2.py
echo "expected: Kirby"
echo "1 2" | python q2.py
echo "expected: Kirby"
echo "2 2" | python q2.py
echo "expected: Dedede"
echo "4 2" | python q2.py
echo "expected: Dedede"
echo "1 50" | python q2.py
echo "expected: Kirby"
echo "2 3" | python q2.py
echo "expected: Kirby"
echo "4 3" | python q2.py
echo "expected: Kirby"
echo "5 2" | python q2.py
echo "expected: Kirby"
+1
View File
@@ -0,0 +1 @@
5 2
+1
View File
@@ -0,0 +1 @@
5 4
+4
View File
@@ -0,0 +1,4 @@
length, n = map(int, input().split(" "))
ans = max(2 * n - length - 1, 0)
print(ans)
+7
View File
@@ -0,0 +1,7 @@
import math
n, c = map(int, input().split())
a = [int(input()) for _ in range(n)]
packs = math.ceil(sum(a) / 10)
print(packs * c)
+5
View File
@@ -0,0 +1,5 @@
4 6
15
8
31
42
+18
View File
@@ -0,0 +1,18 @@
n = int(input())
f = [input() for _ in range(n)]
# Continue your code here and print your final answer!
counts = {}
for l in f:
if l in counts:
counts[l] += 1
else:
counts[l] = 1
mk = ""
ma = -1
for k in counts:
if counts[k] > ma or (counts[k] == ma and k < mk):
mk = k
ma = counts[k]
print(mk)
+11
View File
@@ -0,0 +1,11 @@
10
japan
hongkong
japan
hongkong
france
germany
france
germany
japan
japan
+8
View File
@@ -0,0 +1,8 @@
7
hisss
triiilll
buuurrrbllle
his
trlll
burbble
hello
+21
View File
@@ -0,0 +1,21 @@
import re
t = int(input())
a = [input() for _ in range(t)]
# Continue your code here and print your final answer!
patterns = {
"hiss": re.compile("^hiss+$"),
"trill": re.compile("^tri+ll+$"),
"burble": re.compile("^bu+r+bl+e$"),
}
for line in a:
found = False
for name in patterns:
if patterns[name].match(line):
print(name)
found = True
break
if not found:
print("human noises")
+14
View File
@@ -0,0 +1,14 @@
n = int(input())
s = input()
# Continue your code here and print your final answer!
odd_evens = [ord(c) % 2 for c in list(s)]
odd = 0
even = 0
for x in odd_evens:
if x == 1:
odd += 1
else:
even += 1
print(odd, even)
+23
View File
@@ -0,0 +1,23 @@
11
1
1 2 3 4
1
1 3 4 2
2
1 2 3 4 4 3 2 1
1
1 1 1 3
1
1 7 7 2
1
7 7 2 1
1
7 7 1 2
1
1 1 7 2
1
1 1 2 2
1
1 4 1 2
3
1 2 3 3 2 1 4 5 6 1 1 1
+59
View File
@@ -0,0 +1,59 @@
import math
n = int(input())
ks = [0] * n
temp = []
for i in range(n):
k = int(input())
ks[i] = k
temp.append(list(map(int, input().strip().split(" "))))
# print(ks, temp)
def four_season(k: int, temp: list[int]) -> bool:
# print(k, temp)
max_i = -1
max_v = -math.inf
min_i = -1
min_v = math.inf
if len(temp) == 4:
for i in range(len(temp)):
v = temp[i]
if v > max_v:
max_i = i
max_v = v
if v < min_v:
min_i = i
min_v = v
# print(max_i, max_v, min_i, min_v)
if temp[min_i - 2 % 4] == max_v:
return True
if temp[max_i - 2 % 4] == min_v:
return True
return False
for i in range(k):
new_temp = [0] * 4
for j in range(len(temp)):
idx = (i + j) % len(temp)
# print(temp, idx)
new_temp[j // k] += temp[idx]
# print(new_temp)
if four_season(1, new_temp):
return True
return False
for i in range(n):
if len(temp[i]) / ks[i] != 4:
print("No")
continue
if four_season(ks[i], temp[i]):
print("Yes")
else:
print("No")
+8
View File
@@ -0,0 +1,8 @@
0 0 0
0 0 1
0 1 0
1 0 0
0 1 1
1 0 1
1 1 0
1 1 1
+8
View File
@@ -0,0 +1,8 @@
0 4 0
3 0 0
3 0 6
7 3 6
7 3 0
4 7 0
0 4 6
4 7 6
+7
View File
@@ -0,0 +1,7 @@
sum_x = 0
for i in range(8):
x, _, _ = map(int, input().strip().split(" "))
sum_x += x
print(sum_x / 8)
View File