PYTHON
2025-05-24 19:49:05
发布于:浙江
9阅读
0回复
0点赞
要用functools.lru_cache不然会TLE
from functools import lru_cache
@lru_cache(maxsize=128)
def main():
n, k = map(int, input().split())
weights = list(map(int, input().split()))
left = max(weights)
right = sum(weights)
while left < right:
mid = (left + right) // 2
current_weight = 0
trips = 1
for weight in weights:
if current_weight + weight > mid:
trips += 1
current_weight = weight
else:
current_weight += weight
if trips <= k:
right = mid
else:
left = mid + 1
print(left)
main()
这里空空如也
有帮助,赞一个