1

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?

4

2 回答 2

3

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.

于 2013-10-04T08:47:31.030 回答
0
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=' ')
于 2019-03-29T14:25:38.487 回答