谢谢大佬!清晰易懂!学到了!!!按照这个写了一个python版本: # relation: dp[i][j] = max(dp[i-1][j], dp[i-1][j-v[i]] + v[i] * w[i]), the second one is only when j >= v[i] # base1: dp[..][0] = 0 # base2: dp[0][..] = 0 N, m = map(int, input().split()) # because 每件物品的价格 都是 10 元的整数倍, so compress the search space N = int(N/10) # full cases are 4: primary only, # primary + first accessary, primary + second accessary,, primary + first+ second accessary # therefore, create a space with size 3 for price and weight_value to save the info price = [[0] * 3 for _ in range(61)] weight_value = [[0] * 3 for _ in range(61)] dp = [[0] * (N+1) for _ in range(m+1)] for i in range(1, m+1): x, y, z = map(int, input().split()) x = int(x/10) if z == 0: price[i][0] = x weight_value[i][0] = x * y elif price[z][1] == 0: # because 第 j 行给出了编号为 j-1 的物品的基本数据 price[z][1] = x weight_value[z][1] = x * y else: price[z][2] = x weight_value[z][2] = x * y # print(price) # print(weight_value) for i in range(1, m+1): for j in range(1, N+1): a, b, c, d, e, f = price[i][0], price[i][1], price[i][2], weight_value[i][0], weight_value[i][1], weight_value[i][2] if j >= a: dp[i][j] = max(dp[i-1][j], dp[i-1][j-a]+d) else: dp[i][j] = dp[i-1][j] if j >= a+b: dp[i][j] = max(dp[i][j], dp[i-1][j-a-b]+d+e) else: dp[i][j] = dp[i][j] if j >= a+b+c: dp[i][j] = max(dp[i][j], dp[i-1][j-a-b-c]+d+e+f) else: dp[i][j] = dp[i][j] print(dp[m][N] * 10)
点赞

相关推荐

就用这个吧:支持多益再加一个空气使用费
点赞 评论 收藏
分享
牛客网
牛客企业服务