Algorithm for sharing jobs by two executors

♀尐吖头ヾ 提交于 2019-12-06 16:11:03

问题


I encountered the following problem, while testing some web-site:

John and Mary founded J&M publishing house and bought two old printers to equip it. Now they have their first commercial deal - to print a document consisting of N pages. It appears that printers work at different speed. One produces a page in X seconds and other does it in Y seconds. So now company founders are curious about minimum time they can spend on printing the whole document with two printers.

(taken from here http://codeabbey.com/index/task_view/two-printers)

I thought it is the problem for greedy algorithm, but it is told that N could be up to billion, so there perhaps is some simpler solution which I could not see. Could I come to solution dividing them in proportion to X and Y somehow?


回答1:


This seems to be a math question rather than algorithm question. The job distribution would be YN/(X+Y) pages to X, XN/(X+Y) pages to Y. Total time would be XYN/(X+Y) which is optimal (note that it is equivalent to N/(1/X + 1/Y). Since YN/(X+Y) might not be integer, you can just calculate a few values (if X is rounded up and Y is rounded down, and vice versa) , then take the minimum. Or as you said, you can round down both and give any remaining jobs to the faster machine.




回答2:


for i in range(int(input())):
    x,y,n = list(map(float, input().split()))
    x_1 = int(y*n / (x+y))
    y_1 = int(x*n/ (x+y))
    n = n - (x_1 + y_1)
    x_ans = int(max( (x_1 + n)* x, y_1 * y))
    y_ans = int(max( x_1 * x, (y_1 + n) * y))

    print(min(x_ans,y_ans),end=' ')


来源:https://stackoverflow.com/questions/19176589/algorithm-for-sharing-jobs-by-two-executors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!