【问题标题】:Algorithm for distributing tasks to two printers?将任务分配给两台打印机的算法?
【发布时间】:2016-03-08 13:04:05
【问题描述】:

我在网上做编程练习,发现了这个问题:

两台打印机以不同的速度工作。第一台打印机在x 分钟内打印一张纸,而第二台打印机在y 分钟内完成。总共要打印N 论文,如何将任务分配给这些打印机以使打印时间最短?

这个练习给了我三个输入 x,y,N 并要求输出最短时间。

输入数据:

1 1 5

3 5 4

答案:

3 9

我尝试将第一台打印机的任务设置为a,将第二台打印机的任务设置为N-a。最有效的打印方式是让它们具有相同的时间,因此最短时间为 ((n*b)/(a+b))+1。但是这个公式是错误的。

然后我尝试使用蛮力的方式来解决这个问题。我首先在ab 中区分了哪个更小(更快)。然后我继续向速度更快的打印机添加一张纸。当那台打印机所需的时间比另一台打印机打印一张纸的时间长时,我将一张纸给较慢的打印机,并减去较快的打印机的时间。

代码如下:

def fastest_time (a, b, n):
""" Return the smalles time when keep two machine working at the same time.
    The parameter a and b each should be a float/integer referring to the two
    productivities of two machines. n should be an int, refering to the total 
    number of tasks. Return an int standing for the minimal time needed."""

    # Assign the one-paper-time in terms of the magnitude of it, the reason 
    # for doing that is my algorithm is counting along the faster printer.
    if a > b:
        slower_time_each = a
        faster_time_each = b
    
    elif a < b :
        slower_time_each = b
        faster_time_each = a
    
    # If a and b are the same, then we just run the formula as one printer
    else :
        return (a * n) / 2 + 1
    
    faster_paper = 0
    faster_time = 0
    slower_paper = 0

    # Loop until the total papers satisfy the total task
    while faster_paper + slower_paper < n:
    
        # We keep adding one task to the faster printer
        faster_time += 1 * faster_time_each
        faster_paper += 1
    
        # If the time is exceeding the time needed for the slower machine,
        # we then assign one task to it
        if faster_time >= slower_time_each:
            slower_paper += 1
            faster_time -= 1 * slower_time_each

    # Return the total time needed
    return faster_paper * faster_time_each

N 很小或xy 很大时它可以工作,但是当xy 非常小时它需要很多时间(我猜超过10 分钟)来计算,即输入是1 2 159958878

我相信有更好的算法来解决这个问题,谁能给我一些建议或提示?

【问题讨论】:

  • 检查函数的缩进
  • @anttihaapala 谢谢,已修复
  • 你能举个慢输入的例子吗
  • @anttiHaapala 你的意思是我的程序无法处理的输入?点赞 1 2 159958878
  • 一目了然可以看出最终值是错误的。例如,给定 4 和 7 且 N = 2,打印所需时间为 7,与更快的速度无关。

标签: python algorithm time-complexity distribution


【解决方案1】:

给定表单中的输入

x, y, n = 1, 2, 159958878

这应该可以工作

import math
math.ceil((max((x,y)) / float(x+y)) * n) * min((x,y))

这适用于您的所有示例输入。

In [61]: x, y, n = 1,1,5

In [62]: math.ceil((max((x,y)) / float(x+y)) * n) * min((x,y))
Out[62]: 3.0

In [63]: x, y, n = 3,5,4

In [64]: math.ceil((max((x,y)) / float(x+y)) * n) * min((x,y))
Out[64]: 9.0

In [65]: x, y, n = 1,2,159958878

In [66]: math.ceil((max((x,y)) / float(x+y)) * n) * min((x,y))
Out[66]: 106639252.0

编辑:

这不适用于@Antti 提到的情况,即x, y, n = 4,7,2

原因是我们首先考虑更短的时间。所以解决方法是找到两个值,即考虑更小的时间和考虑更大的时间,然后选择哪个结果值更小。

所以,这适用于所有情况,包括 @Antii's

min((math.ceil((max((x,y)) / float(x+y)) * n) * min((x,y)),
     math.ceil((min((x,y)) / float(x+y)) * n) * max((x,y))))

虽然在某些极端情况下您可能需要稍作改动。

【讨论】:

  • 谢谢!我有点困惑为什么我们可以选择沿着更短的时间或更大的时间进行计算?我可以在这个公式中思考,但不知何故我无法推理它。是否就像将r 文件分配给第一台打印机,将n-r 文件分配给第二台打印机一样。我们的第一个目标是使它们具有相等的时间,即ar = b (n-r),然后我们得到 r = (bn/(a+b))。在对应时间之前,为什么我们使用天花板函数而不是地板函数?
  • 这里我们使用打印机打印一张纸所花费的时间作为一台打印机完成的工作的比例。就像x,y=4,7,那么我们可以说,X 打印机将执行 y/(x+y) 工作,而 Y 打印机将执行 x/(x+y) 工作。现在通常是 x/(x+y) 代表 X 和 y/(x+y) 代表 Y 但是这里因为这些值是花费的时间,我们将计算完成的工作,如果时间更短则会更多,所以我们反转我们就是这样做的。并且,使用天花板功能是因为如果打印机打印的纸张数量是 2.3,那么基本上它会打印 3 张纸张,而另一台打印机将只打印 0.7 张纸张。
猜你喜欢
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多