update README.md & complete 9184.py, 14889.py

This commit is contained in:
2021-02-16 19:58:24 +09:00
parent 4f89ab1c51
commit 1b7ceef1da
3 changed files with 79 additions and 5 deletions

View File

@@ -0,0 +1,42 @@
N = int(input())
S = [list(map(int, input().split())) for _ in range(N)] # S[j][i]
T = [0 for i in range(N)]
U = []
def get_score(i, j):
return S[i][j] + S[j][i]
def get_scores(p):
score = 0
for i in range(N // 2):
for j in range(i + 1, N // 2):
score += get_score(p[i], p[j])
return score
def f(t, l):
T[t] = 1
if l == N // 2 - 1:
ut1, ut2 = [], []
for i, v in enumerate(T):
if v == 0:
ut1.append(i)
else:
ut2.append(i)
u1 = get_scores(ut1)
u2 = get_scores(ut2)
# print(T, ut1, ut2, u1, u2)
U.append(abs(u1 - u2))
return
for i in range(t + 1, N):
T[i] = 1
f(i, l + 1)
T[i] = 0
f(0, 0)
print(min(U))

View File

@@ -0,0 +1,26 @@
Mem = [[[None for _ in range(50)] for _ in range(50)] for _ in range(50)]
def w(a, b, c):
if a <= 0 or b <= 0 or c <= 0:
return 1
elif Mem[a - 1][b - 1][c - 1] is not None:
return Mem[a - 1][b - 1][c - 1]
t = 0
if a > 20 or b > 20 or c > 20:
t = w(20, 20, 20)
elif a < b < c:
t = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c)
else:
t = w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1)
Mem[a - 1][b - 1][c - 1] = t
return t
while True:
a, b, c = map(int, input().split())
if a == b == c == -1:
break
print(f"w({a}, {b}, {c}) = {w(a, b, c)}")