贪心,二分
同类型题:蓝桥杯 算法提高 ADV-1175 打包
python
def judge(x):
wood = 0
max_val = 0
ans_len = 0
for i in ll:
if i > x:
return False
elif max(max_val, i) * (ans_len + 1) <= x:
max_val = max(max_val, i)
ans_len += 1
else:
wood += 1
max_val = i
ans_len = 1
if ans_len:
wood += 1
return wood <= m
n, m = map(int, input().split())
ll = list(map(int, input().split()))
l, r = 0, max(ll) * len(ll)
while l < r:
mid = (l + r) // 2
if judge(mid):
r = mid
else:
l = mid + 1
print(l)
函数遍历中加入了一个剪枝,判断当单个画大于木板时直接返回false,否则代码会直接判断加入新元素后的大小