UVa 10003 - Cutting Sticks

10003 - Cutting Sticks

 * http://acm.uva.es/p/v100/10003.html

Summary
This is a straightforward Dynamic Programming problem. The trick is to define the recurrence carefully.

Explanation
Since $$N \leq 50$$, most reasonable algorithms will breeze through the time limit.

The concept behind the recurrence involves rephrasing it so that it can be answered with subproblems.

Given a series of locations to make the cuts and its borders, what is the cheapest way to make all the cuts? The answer is basically try everything and take the minimum - make a cut everywhere, and try to answer that subproblem that comes up.

So the function $$C( a_1, a_2, \ldots, a_n )$$ where $$a_i$$ are places where cuts are possible. In particular, $$a_1$$ and $$a_n$$ will serve as the end points, since it never makes sense to make a cut at an edge (and thus having the same problem).

When we make a cut at $$a_i$$, it gives us two subproblems - the left side, and the right side. With the base case $$C( a_i, a_{i+1} ) = 0$$ (which basically means if there is one board with no more cuts to be made, the cost is zero), we can now formulate the recurrence:

$$C( a_1, a_2, \ldots, a_n ) = \min_{i \in ( 1, \ldots, n )} \left( C( a_1,\ldots,a_i) + C( a_i,\ldots,a_n ) + cost( a_1,\ldots,a_n ) \right)$$

The cost of making a cut is simply

$$cost( a_1,\ldots,a_n ) = a_n - a_i$$

Input
Input Here

Output
Output Here