Problem :
This is the time of Christmas and Santa wants to distribute gifts to M children. He has N number of boxes, and each box contains some chocolates. Now, Santa wants to distribute N boxes to M children keeping in mind that distribution should be as fair as possible. To fairly distribute the gift boxes, Santa wants to minimize the maximum number of choclates gifted to a child.
Formally, given M number of children and N boxes, where each box contains some amount of chocolates. The task is to minimize the maximum number of chocolate gifted to a child.
In another word, you have to divide array into M subset such that maximum sum of subset is minimized.
Input:
First line of input contains number of testcases T. For each testcase, there will be three lines of input. First line contains N number of gift boxes, next line contains choclates in each of the N boxes. Last line contains number of children.
Output:
For each testcase, print the minimum number of maximum chocolate a child get.
Constraints:
1 <= T <= 100
1 <= N, M <= 10^6
1 <= A[i] <= 10^8
Example:
Input:
1
4
12 34 67 200
3
Output:
200
Explanation:
Testcase 1: Minimum 200 chocolates will be given to a child which gets the maximum number of chocolate.
Another Corner Case : n = 5 and M=3
array is : [ 3 4 5 5 8]
answer is : 9
how to solve? i didn’t figure out.
Auto comment: topic has been updated by n1e2m3 (previous revision, new revision, compare).
Let's turn this into a decision problem — Is it possible to give at most k chocolates to each child? This is np-complete. Because we can reduce decision version of bin packing to this problem (just let a unit of weight be 1 chocolate and the bins be children, also let the capacity of all bins be k). Now the answer for the bin packing decision problem is yes iff the answer for the chocolate decision problem is yes.
If you can solve the original chocolate problem, it is trivial to solve its decision version. So this problem is not solvable (deterministically) with a polynomial time algo (unless P=NP).
thanks, but i have seen some of people are solve this type of problem by using binary search and sorting. link : https://discuss.codechef.com/t/help-needed-standard-dp-backtracking-problem/26576/10
can any one provide more problem like this one?
I am not sure about the correctness of the algo you described.
His algorithm is greedily stuffing the biggest box sizes possible into each child. It seems intuitively wrong, and LanceTheDragonTrainer seems correct. But I can't find a counter case!
upd: i have found a countercase. n1e2m3, this problem is unsolvable in polynomial time and LanceTheDragonTrainer is correct.
his code gives 19, the correct answer is 18 (5 + 3 + 2 + 2 + 2 + 2 + 2 and 12 + 2 + 2 + 2)
Ok, thanks @farmersrice you are correct.