样例错了也能过?
2025-02-22 17:34:29
发布于:北京
10阅读
0回复
0点赞
啊?不愧是你,deepseek!一开始还编译错误了!
下面是代码
import sys
def main():
sys.setrecursionlimit(1 << 25)
N, M = map(int, sys.stdin.readline().split())
equipments = []
for _ in range(N):
parts = sys.stdin.readline().split()
power = int(parts[0])
type_e = parts[1]
if type_e == 'B':
price = int(parts[2])
limit = int(parts[3])
equipments.append( ('B', power, price, limit) )
else:
C = int(parts[2])
components = list(map(int, parts[3:3+2*C]))
childs = []
for i in range(C):
child_type = components[2*i]
child_num = components[2*i +1]
childs.append( (child_type-1, child_num) )
equipments.append( ('A', power, childs) )
req = [dict() for _ in range(N)]
is_basic = [False]*N
for i in range(N):
if equipments[i][0] == 'B':
is_basic[i] = True
memo = [False]*N
def compute_req(e):
if memo[e]:
return
memo[e] = True
if equipments[e][0] == 'B':
req[e][e] = 1
return
_, _, childs = equipments[e]
req[e].clear()
for (child, num) in childs:
compute_req(child)
for k, v in req[child].items():
req[e][k] = req[e].get(k, 0) + num * v
return
for i in range(N):
compute_req(i)
basic_power = []
basic_price = []
basic_limit = []
basic_id = []
for i in range(N):
if is_basic[i]:
basic_id.append(i)
basic_power.append(equipments[i][1])
basic_price.append(equipments[i][2])
basic_limit.append(equipments[i][3])
K = len(basic_id)
extra_power = [0]*(M+1)
for i in range(K):
p = basic_price[i]
w = basic_power[i]
max_num = basic_limit[i]
if p ==0:
continue
num = min(max_num, M//p)
k = 1
while num >0:
k = min(k, num)
cost = k * p
val = k * w
for j in range(M, cost-1, -1):
if extra_power[j - cost] + val > extra_power[j]:
extra_power[j] = extra_power[j - cost] + val
num -= k
k *= 2
dp = [-1]*(M+1)
dp[0] = 0
for i in range(N):
if equipments[i][0] == 'B':
continue
childs = equipments[i][2]
power_i = equipments[i][1]
req_i = req[i]
max_x = float('inf')
for j in basic_id:
if j not in req_i:
continue
cnt_per = req_i[j]
if cnt_per ==0:
continue
limit = basic_limit[basic_id.index(j)]
max_x_i = limit // cnt_per
if max_x_i < max_x:
max_x = max_x_i
if max_x == float('inf'):
max_x = 0
sum_p_per_unit = 0
for j in basic_id:
if j in req_i:
sum_p_per_unit += req_i[j] * basic_price[basic_id.index(j)]
if sum_p_per_unit ==0:
max_x_possible = M
else:
max_x_possible = min(max_x, M // sum_p_per_unit)
max_x = min(max_x, max_x_possible)
if max_x ==0:
items = []
else:
items = []
for x in range(0, max_x+1):
cost = x * sum_p_per_unit
if cost > M:
continue
valid = True
for j in basic_id:
if j in req_i and req_i[j] *x > basic_limit[basic_id.index(j)]:
valid = False
break
if not valid:
continue
power = x * power_i
for j in basic_id:
if j in req_i:
power += req_i[j] * x * basic_power[basic_id.index(j)]
items.append( (cost, power) )
new_dp = dp.copy()
for cost, power in items:
for j in range(M, cost-1, -1):
if dp[j - cost] != -1:
if new_dp[j] < dp[j - cost] + power:
new_dp[j] = dp[j - cost] + power
dp = new_dp
max_total = 0
for c in range(M+1):
if dp[c] == -1:
continue
if c > M:
continue
rem = M - c
if rem <0:
continue
total = dp[c] + extra_power[rem]
if total > max_total:
max_total = total
print(max_total)
if __name__ == "__main__":
main()
这里空空如也
有帮助,赞一个