Painters And Boards

Posted: 18 Nov, 2020
Difficulty: Moderate


Try Problem

Painters in a city are feeling bored, so they have decided to paint all boards which are visible to them. Now, they want to finish this task as soon as possible but they are unable to figure out the minimum time required to complete it. Can you help them out?

You are given ‘N’ boards that are to be painted by ‘A’ painters. Each painter takes ‘B’ units of time to paint one unit of the board. So, if the length of a board is 'X' units, then it will take a painter X * B units of time, to paint the whole board.

Your task is to return the minimum time required to paint all the boards subject to the following conditions-

1. Any painter will only paint contiguous sections of the board. 
   For eg, A configuration where painter 1 paints boards 1 and 3 and not 2 is invalid.
2. A board cannot be painted partially by one painter, and partially by another.
Input Format
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.

The first line of each test case contains three single space-separated integers ‘N’, ‘A’ and ‘B’ denoting the number of boards to be painted, number of painters available and time taken to paint 1 unit of the board, respectively.

The second line of each test case contains N single space-separated integers, denoting the size of each board.
Output Format :
For each test case, print the minimum time required to paint all boards. 

Output for each test case will be printed in a new line. 
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= A <= 10^5
1 <= B <= 10^9
1 <= N <= 10^3
1 <= Board[i] <=10^5

Time Limit: 1sec
Approach 1

We can iterate through the time ‘t’ and check if all the boards can be painted within time ‘t’ using at most A painters. The smallest time t at which we will be able to complete our work will be our answer.


The algorithm will be-

  • Let us assume the time taken to paint 1 unit of each board be 1. Then we can multiply our final answer by ‘B’ to get our original answer.
  • We will start iterating from currTime = 1 and check if a valid assignment is possible.
    • We will start a loop, beginning from i = 0.
    • We will maintain two variables ‘sum’ and ‘minPainters’ initially initialised to 0 and 1 respectively.
    • Then for each iteration-
      • If board[i] >= currTime, we return false.
      • Add board[i] to sum.
      • If sum >= currTime, we will set ‘sum’ to board[i] and increment minPainters by 1.
    • If minPainters is less than equal to ‘A’, we return true. Else, we return false.
Try Problem