Description
Dilpreet wants to paint his dog- Buzo’s home that has n boards with different lengths[A1, A2,…, An]. He hired k painters for this work and each painter takes 1 unit time to paint 1 unit of the board.The problem is to find the minimum time to get this job done under the constraints that any painter will only paint continuous sections of boards, say board {2, 3, 4} or only board {1} or nothing but not board {2, 4, 5}.
Constraints:1<=T<=100,1<=k<=30,1<=n<=50,1<=A[i]<=500
Input
The first line consists of a single integer T, the number of test cases. For each test case, the first line contains an integer k denoting the number of painters and integer n denoting the number of boards. Next line contains n- space separated integers denoting the size of boards.
Output
For each test case, the output is an integer displaying the minimum time for painting that house.
Sample Input 1
2
2 4
10 10 10 10
2 4
10 20 30 40
Sample Output 1
20
60
注:借鉴前辈的基础上写的,太强了,我是想不出来
# 桶可以看作是每个人的时间,求时间最短
# 最小桶容量,最大桶含量,数组,人数
def solution(min_cap, max_cap, l, p):
if min_cap == max_cap:
return min_cap
else:
# 通过二分法求最小桶容量
mid = (min_cap + max_cap) // 2
temp = number(mid, l)
# 桶容量比较大,给定p个桶的情况下可以装完
if temp <= p:
return solution(min_cap, mid, l, p)
# 桶容量比较小,给定p个桶的情况下装不下
else:
return solution(mid + 1, max_cap, l, p)
# 桶容量、数组.返回按照该桶容量放的话,能放几个桶
def number(cap, l):
res = 1
sum = 0
for ele in l:
sum += ele
if sum > cap:
res += 1
sum = ele
return res
if __name__ == '__main__':
num_examples = int(input())
for i in range(num_examples):
l1 = list(map(int, input().strip().split()))
p, length = l1[0], l1[1]
l2 = list(map(int, input().strip().split()))
min_cap = max(l2)
max_cap = sum(l2)
result = solution(min_cap, max_cap, l2, p)
print(result)