Alice has decided to publish ‘X’ different comic books. For this purpose, he has ‘Y’ printing machines and ‘Z’ binding machines. The ‘ith’ printing machine takes ‘printMachine[i]’ minutes to print all pages of a comic book. Each binding machine takes ‘K’ minutes to bind all comic book pages.
At a single time, each machine(a printing or a binding) can process at most the pages of a single comic book.
For publishing a comic book, these steps have to be followed-
1. Start printing the pages of a comic book in an unoccupied ‘ith’ printing machine.
2. After ‘printMachine[i]' minutes, the printed pages are taken from the ‘ith’ printing machine.
3. After a non-negative amount of time, the printed pages of the comic book are placed in an unoccupied binding machine.
4. After ‘K’ minutes, the pages are removed from the binding machine.
Assume that the time is negligible for placing the pages into or removing them from the machines. You need to help Alice find the minimum time to publish X comics.
Example:
X=3, Y=2, Z=2, K=5
printMachine = [5, 7]
We can print the first book in the first printing machine at ‘T=0’, which takes five minutes. After that, we can give it to the first binding machine, which will bind it till ‘T=10’.
Similarly, we can print the second book in the second printing machine at ‘T=0’, which takes seven minutes. After that, we can give it to the second binding machine, which will bind it till ‘T=12’.
Hence, will finish both books in 12 minutes. So, the answer to this example will be 12.
First-line contains 'T,' denoting the number of Test cases.
For each Test case:
The first line contains four integers ‘X’,’ Y’,’ Z’, and ‘K’
The second line contains ‘Y’ integers, the value for the ‘printMachine’ array.
Output format :
For each test case, print a single integer denoting the time taken to print all the books.
Print the output of each test case in a new line.
1 <= T <= 10
1 <= X <= 10^6
1 <= Y <= 10^5
1 <= Z <= 10^9
1 <= K <= 10^9
1 <= printMachine[i] <= 10^9
Time Limit: 1 sec
2
2 1 1 34
1100
2 3 2 10
10 16 1
2234
12
For test case 1:
X=2, Y=1, Z=1, K=34
printMachine = [1100]
We can print the first book in the first printing machine at ‘T=0’, which takes 1100 minutes. After that, we can give it to the first binding machine, which will bind it till ‘T=1134’.
We have to wait for 1100 minutes, then print the second book in the first printing machine at ‘T=1100’, which takes 1100 minutes. After that, we can give it to the binding machine, which will bind it till ‘T=2234’.
Hence, will finish both books by 2234 minutes. So, the answer to this example will be 2234.
For test case 2:
X=2, Y=3, Z=2, K=10
printMachine = [10, 16, 1]
We can print the first book in the third printing machine at ‘T=0’, which takes a time of one minute. After that, we can give it to the first binding machine, which will bind it till ‘T=11’.
We have to wait for 1 minute, then print the second book in the third printing machine at ‘T=1’, which takes a time of one minute. After that, we can give it to the second binding machine, which will bind it till ‘T=12’.
Hence, will finish both books in 12 minutes. So, the answer to this example will be 12.
2
4 3 2 15
3 5 7
5 2 5 100
10 10
35
130
Assign books to a printing machine that can complete them the earliest.
Approach :
Algorithm:
printingEndAt = printingMachine.first
timeToPrint = printingMachine.second
bindingStartAt = max(printingEndAt, bindingMachine)
bindingEndAt = bindingStartAt + K
O( X * (logY + log Z) ) where ‘X’ is the number of books we have to print, ‘Y’ is the number of printing machines we have, and ‘Z’ is the number of binding machines we have.
We are iterating over the number of books ‘X’, and for each book, we are inserting one element into both queues.
Hence, time complexity will be O( X*(LogY + log Z) ).
O( Y + Z ) where ‘X’ is the number of books we have to print and ‘Z’ is the number of binding machines we have.
We are maintaining two queues of size ‘Y’ and ‘Z’ respectively.
Hence space complexity will be O(Y+Z).