(Motivation: Consider a problem where you have to select a sports team from a population of available players. Each player has a certain skill level exactly proportional to their salary expectation, and you want the total of this skill/salary level to exactly match your total salary cap.)
I need to write the following function:
bool possibleAssignment(int N, int M, int T, vector<int> H);
The input constraints are:
0 < N <= 500 < M <= 500 < T <= 2500H.size() == N + 1- Forall
i,0 <= H[i] <= M
possibleAssign returns true iff an array X of M ints can be assigned with the following three constraints:
- Forall
i,0 <= X[i] <= N - Forall
v, the number of elements ofXwith valuevis <= H[v] - The sum of X is T
By what algorithm or method can I implement possibleAssign?
This problem seems reduceable from the Subset Sum Problem, or it is better known variant: knapsack problem, which are NP-Hard, so there is no known polynomial solution for it.
However, it seems T is small enough, and luckily, there is a pseudo polynomial solution to the problem using DP.
Because the problem is already similar to the knapsack problem, I’d try to reduce the problem to fit the knapsack problem, and then invoke the DP algorithm for finding the best solution for the knapsack problem:
I’d first filter the list, keep only H[v] elements with value v. Now, set the elemets as follows:
This will get you going – giving you the maximal number of elements that can be assigned with salary constraint T