math - Algorithm for finding if set of numbers can add up to X, with constrained histogram -
(motivation: consider problem have select sports team population of available players. each player has skill level proportional salary expectation, , want total of skill/salary level match total salary cap.)
i need write following function:
bool possibleassignment(int n, int m, int t, vector<int> h);
the input constraints are:
0 < n <= 50
0 < m <= 50
0 < t <= 2500
h.size() == n + 1
- forall
i
,0 <= h[i] <= m
possibleassign returns true iff array x of m ints can assigned following 3 constraints:
- forall
i
,0 <= x[i] <= n
- forall
v
, number of elements ofx
valuev
<= h[v] - the sum of x t
by algorithm or method can implement possibleassign?
this problem seems reduceable subset sum problem, or better known variant: knapsack problem, np-hard, there no known polynomial solution it.
however, seems t small enough, , luckily, there pseudo polynomial solution problem using dp.
because problem similar knapsack problem, i'd try reduce problem fit knapsack problem, , invoke dp algorithm finding best solution knapsack problem:
i'd first filter list, keep h[v] elements value v. now, set elemets follows:
value(x) = 1 weight(x) = x bag size = t
this going - giving maximal number of elements can assigned salary constraint t
Comments
Post a Comment