There are N pieces each having size Ai. The cost of joining a piece of size x and a piece of size y is abs(x-y). What is the most optimal way to join all the pieces ? Can it be solved using the max-flow algorithm ?
Asked
Active
Viewed 82 times
2
-
1why do you believe that it should be solved with max-flow? Or have you just randomly included some algorithm? – Salvador Dali Mar 06 '16 at 11:41
-
As a competetive programmer with ~3 years of experience, I had an intuitive feeling that the problem could be reduced to the max flow problem in some way. – Tushar Poddar Mar 06 '16 at 13:12
-
Are the sizes all positive? Are they all integers? Is there a maximum size? I ask because, for problems like 0-1-knapsack, a dynamic programming solution can work quickly if the maximum value is polylogarithmic in the number of values. My intuition is that, if the sizes are all positive (even if they are unbounded in size), there is a greedy solution to the problem in O(n lg n): join the smallest two pieces. Then find the next two smallest pieces (which could possibly include the result of your previous join) and join them, and repeat. I don't have a proof yet. – jbapple Mar 06 '16 at 17:58
-
Yes, all the sizes are positive integers <=100. And there are at most 100 pieces. @jbapple – Tushar Poddar Mar 06 '16 at 19:04
-
This is an interesting algorithm question that might be better suited for https://cs.stackexchange.com, which has more of a theory bent. – jbapple Mar 07 '16 at 14:11
-
1@jbapple: The algorithm you describe doesn't always work. For example: 3,4,5,6. You can do it with total cost 4, but your approach results in 6. – Eyal Schneider Mar 08 '16 at 00:07