5 KiB
slug | title | author | order | prerequisites | ||||
---|---|---|---|---|---|---|---|---|
/gold/dp | Introduction to Dynamic Programming | Michael Cao | 1 |
|
An introduction to dynamic programming concepts needed for USACO Gold.
Dynamic programming is a very important concept which emerges in the USACO gold division and extends to the IOI. Typically, one or two problems from every contest in the gold division uses some sort of Dynamic programming.
Tutorial
The following tutorials serve as an introduction into the mindset of Dynamic programming.
- CPH 7
- great introduction that covers most classical problems
- Topcoder DP
- great for all skill levels
- CPC.6
- examples with nonclassical problems
- HackerRank DP
- also covers many classical problems
Practice makes perfect. Start by doing some classical problems (try at least one of each), as these are must know DP problems. Each topic starts with direct applications of the classical problems, and then some interesting variations and USACO problems which utilize the ideas. Solutions for most problems (excluding USACO) can be found on Chapter 7 of CPH.
General
- Atcoder DP Contest
- very good!
- CSES DP Section
- also very good!
- Codeforces DP Problem List
Coin Change / Knapsack
(description)
- Classic
- Unbounded
- 0/1
- Large Capacity + Small Values
- Reconsider the state.
- USACO Gold
Paths on Grid (& Related)
- Classic
- Longest Common Subsequence
- Edit Distance
- Paths on a Grid
- USACO
- Gold - Cow Checklist
dp[visited i Hs][visited j Gs][last cow visited] -> min energy
- Gold - Radio Contact
- similar to above
- Gold - Why Did The Cow Cross the Road II
- variation on LCS
- Old Silver - Landscaping
- Although the problem looks different, this is actually a direct application of edit distance.
- Old Gold - Palindromic Paths
- Consider the answer. What are some properties of it?
- Gold - Cow Checklist
Longest Increasing Subsequence
- LIS in Quadratic Time
- Try to improve to
O(N\log N)
.
- Try to improve to
- Sort It Out (USACO Platinum)
- Challenging!
Additional USACO Problems
- Fairly Straightforward
- Hoof Paper Scissors
dp[first i games][# changes][last gesture ] -> max games won
- Time is Mooney
dp[time][city] -> money
- Teamwork
O(NK^2)\to O(NK)
- Hoof Paper Scissors
- Trickier
- Mortal Cowmbat
- Use Floyd-Warshall, Prefix Sums
dp[first i letters form valid combo][last letter] -> time
- Snakes
dp[first m groups][k changes] -> total sum of net sizes
- Taming the Herd
dp[consider first i entries only][last breakout in first i occurs at j][k breakouts among first i entries] -> # changes
- Stamp Painting
- must be
K
consecutive with same color O(NK)\to O(N)
- must be
- Circular Barn Revisited
- can brute force make your DP easier? (yes)
- Mortal Cowmbat