题目列表
乌龟棋代码。四重for循环。主要是idx函数。
经过测试。1e6的数组在pypy3里面大概是20MB左右,还是不会MLE的。
import sys
# sys.stdin = open('./../../input.txt', 'r')
I = lambda: int(input())
MI = lambda: map(int, input().split())
LI = lambda: list(map(int, input().split()))
n, m = MI()
a = LI()
c = LI()
cc = [0] * 4
for x in c: cc[x - 1] += 1
D1, D2, D3, D4 = (x + 1 for x in cc)
idx = lambda x1, x2, x3, x4: x1 + x2 * D1 + x3 * D1 * D2 + x4 * D1 * D2 * D3
N = D1 * D2 * D3 * D4
dp = [0] * N
dp[0] = a[0]
for i in range(D1):
for j in range(D2):
for k in range(D3):
for l in range(D4):
d = i + j * 2 + k * 3 + l * 4
if i != 0:
dp[idx(i, j, k, l)] = max(dp[idx(i, j, k, l)], dp[(idx(i - 1, j, k, l))] + a[d])
if j != 0:
dp[idx(i, j, k, l)] = max(dp[idx(i, j, k, l)], dp[(idx(i, j - 1, k, l))] + a[d])
if k != 0:
dp[idx(i, j, k, l)] = max(dp[idx(i, j, k, l)], dp[(idx(i, j, k - 1, l))] + a[d])
if l != 0:
dp[idx(i, j, k, l)] = max(dp[idx(i, j, k, l)], dp[(idx(i, j, k, l - 1))] + a[d])
# print(dp)
print(dp[-1])
第二题,达到指定参数且成本最小。本题使用的字典和元组来访问高维数组。
数据范围是6**5*10左右。
import sys
sys.stdin = open('./../../input.txt', 'r')
I = lambda: int(input())
MI = lambda: map(int, input().split())
LI = lambda: list(map(int, input().split()))
n, K, P = MI()
mtx = [LI() for _ in range(n)]
dp = dict()
dp[tuple([0] * K)] = 0
for i in range(n):
cost = mtx[i][0]
xs = mtx[i][1:]
ndp = dict()
for k, v in dp.items():
nk = [0] * K
for i in range(K):
nk[i] = min(k[i] + xs[i], P)
nk = tuple(nk)
if nk in ndp:
ndp[nk] = min(dp[k] + cost, ndp[nk])
else:
ndp[nk] = dp[k] + cost
for k, v in ndp.items():
if k in dp:
dp[k] = min(dp[k], v)
else:
dp[k] = v
ans = float('inf')
for k, v in dp.items():
if all(k[i] >= P for i in range(K)):
ans = min(ans, dp[k])
print(ans if ans < float('inf') else -1)