Click this box to toggle showing all answers!
5 + 5 > 1 + 8 = 0 + 9 ⇒ 10
Who knows! Use DP!
length $i$ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
price $p_i$ | 1 | 5 | 8 | 9 | 10 | 17 | 17 | 20 |
Cut Lengths | Value |
4 (ie no cuts) | 9 |
1, 3 | 1 + 8 = 9 |
2, 2 | 5 + 5 = 10 |
3, 1 | 8 + 1 = 9 |
1, 1, 2 | 1 + 1 + 5 = 7 |
1, 2, 1 | 1 + 5 + 1 = 7 |
2, 1, 1 | 5 + 1 + 1 = 7 |
1, 1, 1, 1 | 1 + 1 + 1 + 1 = 4 |
After each inch. Can cut rod in $2^{n-1}$ ways since each inch can have a cut or no cut
Can cut rod in $2^{n-1}$ ways since each inch can have a cut or no cut
123 | Cut Lengths | Value |
000 | 4 (ie no cuts) | 9 |
100 | 1, 3 | 1 + 8 = 9 |
010 | 2, 2 | 5 + 5 = 10 |
001 | 3, 1 | 8 + 1 = 9 |
110 | 1, 1, 2 | 1 + 1 + 5 = 7 |
101 | 1, 2, 1 | 1 + 5 + 1 = 7 |
011 | 2, 1, 1 | 5 + 1 + 1 = 7 |
111 | 1, 1, 1, 1 | 1 + 1 + 1 + 1 = 4 |
length $i$ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
price $p_i$ | 1 | 5 | 8 | 9 | 10 | 17 | 17 | 20 |
$i$ | $r_i$ | Where to cut |
1 | 1 | 1 (no cuts) |
2 | 5 | 2 (no cuts) |
3 | 8 | 3 (no cuts) |
4 | 10 | 2 + 2 |
5 | 13 | 2 + 3 |
6 | 17 | 6 (no cuts) |
7 | 18 | 1 + 6 or 2 + 2 + 3 |
8 | 22 | 2 + 6 |
All start with a cut of 1, followed by all of the ways of cutting rod of length 3
We can look up best way to cut length 3 and all we need to compare is sums of pairs of $r_i$!
You might have. The integer partitions of 4 are: 4, 3+1, 2+2, 2+1+1, 1+1+1. So those sums are all orderings of the partitions of 4.
$i$ | $r_i$ | Maximum of |
0 | $r_0$ | 0 |
1 | $r_1$ | $p_1+r_0 $ |
2 | $r_2$ | $p_1+r_1, p_2+r_0 $ |
3 | $r_3$ | $p_1+r_2, p_2+r_1, p_3+r_0 $ |
4 | 10 | $r_4$$p_1+r_3, p_2+r_2, p_3+r_1,p_4+r_0$ |
... | ... | ... |
length $i$ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
price $p_i$ | 1 | 5 | 8 | 9 | 10 | 17 | 17 | 20 |
$i$ | $r_i$ | optimal solution |
1 | 1 | 1 (no cuts) |
2 | 5 | 2 (no cuts) |
3 | 8 | 3 (no cuts) |
4 | 10 | 2 + 2 |
5 | 13 | 2 + 3 |
6 | 17 | 6 (no cuts) |
7 | 18 | 1 + 6 or 2 + 2 + 3 |
8 | 22 | 2 + 6 |
They all sum to the same thing (ie either 4 or 5)
-- price array p, length n Cut-Rod(p, n) if n = 0 then return 0 end if q := MinInt for i in 1 .. n loop q := max(q, p(i) + Cut-Rod(p, n-i) end loop return q
MemoizedCutRod(p, n) r: array(0..n) := (0 => 0, others =>MinInt) return MemoizedCutRodAux(p, n, r) MemoizedCutRodAux(p, n, r) if r(n) = MinInt then -- calculate a new solution? q: int := MinInt for i in 1 .. n loop q := max(q, p(i) + MemoizedCutRodAux(p, n-i, r)) end loop end if r(n) := q end if return r(n)
BottomUpCutRod(p, n) r: array(0..n) -- optimal value for rods of length 0..n r(0) := 0 for j in 1 .. n loop q := MinInt for i in 1 .. j loop -- Find the max cut position for length j q := max(q, p(i) + r(j-i) end loop r(j) := q end loop return r(n)
ExtendedBottomUpCutRod(p, n) r: array(0..n) -- optimal value for rods of length 0..n s: array(0..n) -- optimal first cut for rods of length 0..n r(0) := 0 for j in 1 .. n loop q := MinInt for i in 1 .. j loop -- Find the max cut position for length j if q < p(i) + r(j-i) then q := p(i) + r(j-i) s(j) := i -- Remember the value of best so far value of i end if end loop r(j) := q end loop return r and s PrintCutRodSolution(p, n) (r, s) := ExtendedBottomUpCutRod(p, n) while n > 0 loop print s(n) n := n - s(n) end loop
i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
$p_i$ | 0 | 1 | 5 | 8 | 9 | 10 | 17 | 17 | 20 |
ri | 0 | 1 | 5 | 8 | 10 | 13 | 17 | 18 | 22 |
si | 0 | 1 | 2 | 3 | 2 | 2 | 6 | 1 | 2 |
length $i$ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
price $p_i$ | 1 | 5 | 8 | 9 | 10 | 17 | 17 | 20 |
Click this box to toggle showing all answers!