通用一般算法
def total_time(m,n,num):
dp = [0] * m
num.sort()
#print(dp)
if m >= n:
return max(num)
else:
for i in range(0, n, m):
#print(num[i:i + m])
lis = num[i:i + m]
for j in range(len(lis)):
dp[j] += lis[j]
return max(dp)
创新算法
idea
对所有工作按耗时排序之后,流水线工作时间由最后一个任务,即耗时最长的任务决定。而排序之后的任务组合加入流水线的方式由流水线个数m和
需要完成的作业数n即可确定。
代码实现
def total_time0(m,n,num):
num.sort()
t=[num[i] for i in range(n-1,0,-m)]
return sum(t)
对比验证
import random
def total_time(m,n,num):
dp = [0] * m
num.sort()
#print(dp)
if m >= n:
return max(num)
else:
for i in range(0, n, m):
#print(num[i:i + m])
lis = num[i:i + m]
for j in range(len(lis)):
dp[j] += lis[j]
return max(dp)
def total_time0(m,n,num):
num.sort()
t=[num[i] for i in range(n-1,0,-m)]
return sum(t)
if __name__=='__main__':
'''m, n = map(int, input().split())
num = list(map(int, input().split()))'''
m,n = int(random.randint(1,99)),int(random.randint(1,99))
num = [random.randint(0,99) for i in range(0,n)]
#print(m,n,'\n',num)
print(total_time(m,n,num))
print(total_time0(m,n,num))