How can this problem be solved?

Revision en1, by saytzeff, 2018-02-09 22:10:07

You are given N stones, labeled from 1 to N. The i-th stone has the weight W[i]. There are M colors, labeled by integers from 1 to M. The i-th stone has the color C[i] (of course, an integer between 1 to M, inclusive). You want to fill a Knapsack with these stones. The Knapsack can hold a total weight of X. You want to select exactly M stones; one of each color. The sum of the weights of the stones must not exceed X. Since you paid a premium for a Knapsack with capacity X (as opposed to a Knapsack with a lower capacity), you want to fill the Knapsack as much as possible. Write a program that takes all the above values as input and calculates the best way to fill the Knapsack – that is, the way that minimizes the unused capacity. Output this unused capacity. See the explanation of the sample test cases for clarity. Input The first line of input contains the integer T, the number of test cases. Then follows the description of T test cases. The first line of each test case contains three integers, N, M and X, separated by singlespace. The next line contains N integers, W[1], W[2], W[3] … W[N], separated by single space. The next line contains N integers C[1], C[2], C[3] … C[N], separated by single space. Output An optimal way of filling the Knapsack minimizes unused capacity. There may be several optimal ways of filling the Knapsack. Output the unused capacity of the Knapsack (a single integer on a line by itself) for an optimal way. If there is no way to fill the Knapsack, output -1. Output T lines, one for each test case. Constraints 1 ≤ T ≤ 10 1 ≤ M ≤ 100 M ≤ N ≤ 100 1 ≤ W[i] ≤ 100 1 ≤ C[i] ≤ M 1 ≤ X ≤ 10000 Sample Input 4 9 3 10 2 3 4 2 3 4 2 3 4 1 1 1 2 2 2 3 3 3 9 3 10 1 3 5 1 3 5 1 3 5 1 1 1 2 2 2 3 3 3 3 3 10 3 4 4 1 2 3 3 3 10 3 3 3 1 2 1 Sample Output 0 1 -1 -1 Explanation In the first test case you can select stone 2, stone 5 and stone 9. The knapsack will be completely full. Of course, there are several other ways to select stones such that the knapsack is full. The unused capacity in all such ways is 0. In the second test case you cannot select stones such that the knapsack is completely full. You can select stones {1, 4, 9}, such that the unused capacity is 10-1-1-5 = 3. But there is a better way. Select stones {2, 5, 8}. The unused capacity is 10-3-3-3 = 1. This is the optimal way. There is another way that is optimal. Select stones {1, 5, 9}. The unused capacity is 10-1-3-5 = 1. In the third test case there is only one option. Select stones {1, 2, 3}. The total weight will be 11. This is more than what the knapsack can hold. In the fourth test case there is no stone of color 3. Thus, there is no valid selection of stones possible.

Is this possible using only two states dp? I think it might need three states- for ith element, color c, and knapsack capacity X.

Tags #dp, 2d-dp, 3-d dp, knapsack, #implementation

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English saytzeff 2018-02-09 22:13:08 30 (published)
en3 English saytzeff 2018-02-09 22:12:26 0 Tiny change: ' 3\n3 3 10\n3 4 4\n1 2' -> ' 3\n3 3 103 4 4\n1 2' (saved to drafts)
en2 English saytzeff 2018-02-09 22:11:27 910
en1 English saytzeff 2018-02-09 22:10:07 2889 Initial revision (published)