Advanced DP
Created: 2019-11-30 Sat 12:03
Objectives
- Combine bitmask and DP
- Review some methods for solving DP with a limited amount of memory
- Reduce state size by omitting recoverable information
Bitmasks
- Example problem:
- given a collection of objects (up to 20)
- combining different pairs gives different values
- want minimal value of combining all objects into pairs
- Technique:
- Use bits 1 to 20 to store which objects are paired
- e.g., to pair \(i\) with \(j\), do
bitmask = bitmask | (1 << i) | (1 << j)
- Notice that index is counted from the right
- Use
bitmask
as the index to your DP array.
Other Tricks
- Negative Index
- In C++, negative indices are tricky.
- Solution: use an offset
- Running out of memory?
- Consider
map
instead of vector
- Check your representation
Dropping one parameter
- If your state has \(n\) parameters, sometimes you can recover one
parameter from the others.
- E.g., DP over a rectangle; need height, width, area.