-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkeyboards_in_concert.py
102 lines (79 loc) · 2.74 KB
/
keyboards_in_concert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
Decision point: Use the current keyboard, or move to a different one
"""
inf = int(1e9)
def solve_top_down():
min_result = inf
memo = {}
for i in range(n):
min_result = min(
min_result,
top_down(0, i, memo)
)
print(min_result)
# for k, v in memo.items():
# print("{}={}".format(k, v))
def top_down(n_index, k_index, memo):
"""
memo[(n_index,k_index)] = minimum number of switches required to play everything from
notes[n_index] to notes[len(notes)-1] if you start at keyboard[k_index]
"""
if n_index >= len(notes):
return 0
if (n_index, k_index) in memo:
return memo[(n_index, k_index)]
if keyboard_cache[k_index][notes[n_index]]:
result = top_down(n_index + 1, k_index, memo)
memo[(n_index, k_index)] = result
return memo[(n_index, k_index)]
else:
min_switches = inf
for other_k_index in range(n):
if other_k_index == k_index:
continue
if keyboard_cache[other_k_index][notes[n_index]]:
min_switches = min(
min_switches,
top_down(n_index, other_k_index, memo) + 1
)
memo[(n_index, k_index)] = min_switches
return memo[(n_index, k_index)]
def solve_bottom_up():
dp = [[inf] * n for _ in range(len(notes))]
for k_index in range(n):
dp[-1][k_index] = 0 if keyboard_cache[k_index][notes[-1]] else 1
for n_index in range(len(notes) - 2, -1, -1):
# if a keyboard can play the current note, assign its value as
# dp[n_index+1][k_index]
for k_index in range(n):
if keyboard_cache[k_index][notes[n_index]]:
dp[n_index][k_index] = dp[n_index + 1][k_index]
for k_index in range(n):
if dp[n_index][k_index] < inf:
continue
min_switches = inf
for other_k_index in range(n):
if other_k_index == k_index:
continue
if keyboard_cache[other_k_index][notes[n_index]]:
min_switches = min(
min_switches,
dp[n_index][other_k_index] + 1
)
dp[n_index][k_index] = min_switches
min_result = inf
for i in range(n):
min_result = min(
min_result,
dp[0][i]
)
print(min_result)
n, m = list(map(int, input().split()))
keyboard_cache = [[False] * (m + 1) for _ in range(n + 1)]
for i in range(n):
notes = list(map(int, input().split()))[1:]
for note in notes:
keyboard_cache[i][note] = True
notes = list(map(int, input().split()))
solve_top_down()
# solve_bottom_up()