2020-06-04 01:42:57 +00:00
---
2020-06-15 23:19:07 +00:00
id: dp
2020-06-09 16:36:07 +00:00
title: "Introduction to Dynamic Programming"
2020-06-04 01:42:57 +00:00
author: Michael Cao
2020-06-04 22:32:02 +00:00
prerequisites:
2020-06-04 22:43:34 +00:00
-
- Recursion
2020-06-05 00:21:03 +00:00
-
- Silver - Prefix Sums
2020-06-22 19:59:16 +00:00
description: A very important concept which emerges in the Gold division and extends to the IOI. (improve?)
2020-06-04 01:42:57 +00:00
---
2020-06-22 04:00:36 +00:00
< info-block title = "Contest Tip" >
2020-06-22 14:26:06 +00:00
Usually, at least one problem from every gold division contest involves some sort of DP.
2020-06-22 04:00:36 +00:00
< / info-block >
2020-06-09 16:36:07 +00:00
## Tutorial
2020-06-03 21:42:26 +00:00
2020-06-11 00:25:30 +00:00
The following tutorials serve as an introduction into the mindset of DP.
2020-06-04 22:17:28 +00:00
2020-06-09 16:36:07 +00:00
- CPH 7
- great introduction that covers most classical problems
- [Topcoder DP ](https://www.topcoder.com/community/competitive-programming/tutorials/dynamic-programming-from-novice-to-advanced/ )
- great for all skill levels
- [CPC.6 ](https://github.com/SuprDewd/T-414-AFLV/tree/master/06_dynamic_programming )
- examples with nonclassical problems
2020-06-04 21:42:30 +00:00
- [HackerRank DP ](https://www.hackerrank.com/topics/dynamic-programming )
2020-06-09 16:36:07 +00:00
- 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
2020-06-03 19:14:28 +00:00
2020-06-16 23:14:17 +00:00
- [**Atcoder DP Contest** ](https://atcoder.jp/contests/dp/tasks )
- very good!
- [CSES DP Section ](https://cses.fi/problemset/list/ )
- also very good!
- [Codeforces DP Problem List ](http://codeforces.com/blog/entry/325 )
2020-06-03 21:42:26 +00:00
2020-06-11 00:59:01 +00:00
The following USACO problems don't fall into any of the categories below. Arranged roughly in order of difficulty.
2020-06-22 04:00:36 +00:00
< info-block title = "Pro Tip" >
Sometimes it's a good idea to write a slower polynomial-time solution and then optimize it to the desired complexity (say, write $O(N^2)$ first and then speed it up to $O(N)$).
< / info-block >
2020-06-11 00:49:01 +00:00
2020-06-16 23:14:17 +00:00
- [Hoof Paper Scissors ](http://www.usaco.org/index.php?page=viewproblem2&cpid=694 )
- `dp[first i games][# changes][last gesture] -> max games won`
- [Time is Mooney ](http://www.usaco.org/index.php?page=viewproblem2&cpid=993 )
- `dp[time][city] -> money`
- [Teamwork ](http://usaco.org/index.php?page=viewproblem2&cpid=863 )
- $O(NK^2)\to O(NK)$
- [Snakes ](http://www.usaco.org/index.php?page=viewproblem2&cpid=945 )
- `dp[first m groups][k changes] -> total sum of net sizes`
- $O(N^4)\to O(N^3)$
- [Circular Barn Revisited ](http://www.usaco.org/index.php?page=viewproblem2&cpid=622 )
- can brute force make your DP easier? (yes)
- [Taming the Herd ](http://www.usaco.org/index.php?page=viewproblem2&cpid=815 )
- `dp[consider first i entries only][last breakout in first i occurs at j][k breakouts among first i entries] -> # changes`
- [Mortal Cowmbat ](http://usaco.org/index.php?page=viewproblem2&cpid=971 )
- Use Floyd-Warshall, Prefix Sums
- `dp[first i letters form valid combo][last letter] -> time`
- [Stamp Painting ](http://www.usaco.org/index.php?page=viewproblem2&cpid=791 )
- must be $K$ consecutive with same color
- $O(NK)\to O(N)$
2020-06-11 00:49:01 +00:00
2020-06-11 00:25:30 +00:00
## Bounded and Unbounded Knapsack
2020-06-09 16:36:07 +00:00
2020-06-16 23:14:17 +00:00
- Classic
- [Unbounded Knapsack ](https://www.hackerrank.com/challenges/unbounded-knapsack/problem )
- [Unordered Coin Change ](https://cses.fi/problemset/task/1635 )
- [Ordered Coin Change ](https://cses.fi/problemset/task/1636 )
- [Minimum Coins ](https://cses.fi/problemset/task/1634 )
- [0/1 ](https://www.hackerrank.com/contests/srin-aadc03/challenges/classic-01-knapsack/problem )
- [Large Capacity + Small Values ](https://atcoder.jp/contests/dp/tasks/dp_e )
- Reconsider the state.
- USACO Gold
- [Fruit Feast ](http://www.usaco.org/index.php?page=viewproblem2&cpid=574 )
- `dp[fullness] = whether you can achieve this fullness`
- [Talent Show ](http://www.usaco.org/index.php?page=viewproblem2&cpid=839 )
- binary search + knapsack on weight
- CF
- [Round Subset ](http://codeforces.com/contest/837/problem/D ) [](59)
- [Fire ](http://codeforces.com/contest/864/problem/E ) [](59)
2020-06-09 16:36:07 +00:00
## Paths on Grid (& Related)
2020-06-16 23:14:17 +00:00
- Classic
- Longest Common Subsequence
- [Standard ](https://leetcode.com/problems/longest-common-subsequence/ )
- Edit Distance
- [Standard ](https://www.hackerrank.com/contests/cse-830-homework-3/challenges/edit-distance )
- Paths on a Grid
- [Count Paths ](https://atcoder.jp/contests/dp/tasks/dp_h )
- USACO
- [Gold - Cow Checklist ](http://www.usaco.org/index.php?page=viewproblem2&cpid=670 )
- `dp[visited i Hs][visited j Gs][last cow visited] -> min energy`
- [Gold - Radio Contact ](http://www.usaco.org/index.php?page=viewproblem2&cpid=598 )
- similar to above
- [Gold - Why Did The Cow Cross the Road II ](http://www.usaco.org/index.php?page=viewproblem2&cpid=718 )
- variation on LCS
- [Old Silver - Landscaping ](http://www.usaco.org/index.php?page=viewproblem2&cpid=126 )
- Although the problem looks different, this is actually a direct application of edit distance.
- [Old Gold - Palindromic Paths ](http://www.usaco.org/index.php?page=viewproblem2&cpid=553 )
- What are some properties of the answer?
- Other
- [TC Interleaving Parentheses ](https://community.topcoder.com/stat?c=problem_statement&pm=14635&rd=16933 )
- [K-Ordered LCS ](https://www.hackerearth.com/problem/algorithm/mancunian-and-k-ordered-lcs-e6a4b8c6/ )
- [CSA Wrong Brackets ](https://csacademy.com/contest/round-51/task/wrong-brackets/ ) [](69)
2020-06-09 16:36:07 +00:00
## Longest Increasing Subsequence
2020-06-11 00:25:30 +00:00
(add?)
2020-06-16 23:14:17 +00:00
- [LIS in Quadratic Time ](https://leetcode.com/problems/longest-increasing-subsequence/ )
2020-06-22 01:45:24 +00:00
- Try to improve to $O(N\log N)$
- [Solution ](https://cp-algorithms.com/sequences/longest_increasing_subsequence.html ).
- [USACO Old Bronze Cowjog ](http://www.usaco.org/index.php?page=viewproblem2&cpid=489 )
2020-06-16 23:14:17 +00:00
- Not so easy to see, but direct application of LIS.
- [Sort It Out (USACO Platinum) ](http://www.usaco.org/index.php?page=viewproblem2&cpid=865 )
- Challenging!
2020-06-22 01:45:24 +00:00
## Other
- [CSES String Removals ](https://cses.fi/problemset/task/1149 )