separate DP

This commit is contained in:
Benjamin Qi 2020-06-09 12:36:07 -04:00
parent 3017746873
commit 53055e79be
4 changed files with 152 additions and 88 deletions

View file

@ -1,116 +1,109 @@
---
slug: /gold/dp
title: "Dynamic Programming"
title: "Introduction to Dynamic Programming"
author: Michael Cao
order: 1
prerequisites:
-
- Recursion
-
- Bit Operations (for Bitmask DP)
-
- Silver - Prefix Sums
-
- Silver - Depth First Search (for Tree DP)
---
An introduction to Dynamic Programming concepts needed (and not-so-needed but still useful) for USACO Gold.
An introduction to dynamic programming concepts needed for USACO Gold.
<!-- END DESCRIPTION -->
## Introduction to DP
**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.
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. The following tutorials serve as an introduction into the mindset of Dynamic programming.
## Tutorial
- CPH 7 (Great introduction that covers most classical problems)
- [Topcoder DP (Great for all skill levels)](https://www.topcoder.com/community/competitive-programming/tutorials/dynamic-programming-from-novice-to-advanced/)
- [CPC.6 (Examples with nonclassical problems)](https://github.com/SuprDewd/T-414-AFLV/tree/master/06_dynamic_programming)
The following tutorials serve as an introduction into the mindset of Dynamic programming.
- 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
- [HackerRank DP](https://www.hackerrank.com/topics/dynamic-programming)
- also covers many classical problems
## Classical DP 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.
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. Tutorials for most problems (excluding USACO) can be found on Chapter 7 of CPH.
## General
* Knapsack
* [**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)
## Coin Change / Knapsack
(description)
* Classic
* [Unbounded](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.
* [Talent Show (USACO Gold)](http://www.usaco.org/index.php?page=viewproblem2&cpid=839)
* [Fruit Feast (USACO Gold)](http://www.usaco.org/index.php?page=viewproblem2&cpid=574)
* Longest Increasing Subsequence
* [LIS in Quadratic Time](https://leetcode.com/problems/longest-increasing-subsequence/)
* Try to improve to $O(N\log N)$.
* [Sort It Out (USACO Platinum)](http://www.usaco.org/index.php?page=viewproblem2&cpid=865)
* Challenging!
* Coin Change
* [Unordered](https://cses.fi/problemset/task/1635)
* [Ordered](https://cses.fi/problemset/task/1636)
* [Minimum Coins](https://cses.fi/problemset/task/1634)
* Paths on a Grid
* [Count Paths](https://atcoder.jp/contests/dp/tasks/dp_h)
* [Palindromic Paths (Old USACO Gold)](http://www.usaco.org/index.php?page=viewproblem2&cpid=553)
* Consider the answer. What are some properties of it?
* Edit Distance
* [Standard](https://www.hackerrank.com/contests/cse-830-homework-3/challenges/edit-distance)
* [Landscaping (Silver)](http://www.usaco.org/index.php?page=viewproblem2&cpid=126)
* Although the problem looks different, this is actually a direct application of Edit Distance.
* Longest Common Subsequence
* [Standard](https://leetcode.com/problems/longest-common-subsequence/)
## Tree DP
Assumes familiarity with the structures of trees, and basic algorithms to traverse them (eg. dfs) to perform the DP.
* Tutorial
* [DP on Trees (Codeforces)](https://codeforces.com/blog/entry/20935)
* Add more?
* Problems
* [Subtree](https://atcoder.jp/contests/dp/tasks/dp_v)
* [Independent Set](https://atcoder.jp/contests/dp/tasks/dp_p)
* [Barn Painting (USACO Gold)](http://www.usaco.org/index.php?page=viewproblem2&cpid=766)
* Similar to Independent Set
* [Delegation (USACO Gold)](http://usaco.org/index.php?page=viewproblem2&cpid=1019)
* Good rule of solving DP problems applies here: don't just dive into trying to figure out a DP state and transitions, and make some observations about the problem first.
## Bitmask DP
Note: Has not appeared on recent USACO.
* Tutorials:
* CPH Chapter 10
* [Dynamic Programming Over Subsets (Codeforces)](https://codeforces.com/blog/entry/337)
* [Dynamic Programming and Bit Masking (HackerEarth)](https://www.hackerearth.com/practice/algorithms/dynamic-programming/bit-masking/tutorial/)
* Problems:
* [Moovie Moving (Old USACO Gold)](http://www.usaco.org/index.php?page=viewproblem2&cpid=515)
* [Matching](https://atcoder.jp/contests/dp/tasks/dp_o)
* [Square Subsets](https://codeforces.com/contest/895/problem/C)
* [Guards in the Storehouse](https://codeforces.com/problemset/problem/845/F)
## Practice Problems
* **[Atcoder DP Contest (Extremely Good)](https://atcoder.jp/contests/dp/tasks)**
* [CSES DP Section](https://cses.fi/problemset/list/)
* [Codeforces DP Problem List](http://codeforces.com/blog/entry/325)
* USACO (Not Ordered By Difficulty)
* [Time is Mooney](http://www.usaco.org/index.php?page=viewproblem2&cpid=993)
* Graph
* [Mortal Cowmbat](http://usaco.org/index.php?page=viewproblem2&cpid=971)
* with Prefix Sums
* [Painting the Barn](http://usaco.org/index.php?page=viewproblem2&cpid=923)
* with 2D Prefix Sums
* [Snakes](http://www.usaco.org/index.php?page=viewproblem2&cpid=945)
* USACO Gold
* [Talent Show](http://www.usaco.org/index.php?page=viewproblem2&cpid=839)
* [Fruit Feast](http://www.usaco.org/index.php?page=viewproblem2&cpid=574)
* [Cow Poetry](http://usaco.org/index.php?page=viewproblem2&cpid=897)
* with fast exponentiation
* [Teamwork](http://usaco.org/index.php?page=viewproblem2&cpid=863)
* [Taming the Herd](http://www.usaco.org/index.php?page=viewproblem2&cpid=815)
* [Stamp Painting](http://www.usaco.org/index.php?page=viewproblem2&cpid=791)
* [Why Did The Cow Cross the Road I](http://www.usaco.org/index.php?page=viewproblem2&cpid=717)
* [Why Did The Cow Cross the Road II](http://www.usaco.org/index.php?page=viewproblem2&cpid=718)
* needs fast exponentiation for full points
## Paths on Grid (& Related)
* 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)
* Consider the answer. What are some properties of it?
## Longest Increasing Subsequence
* [LIS in Quadratic Time](https://leetcode.com/problems/longest-increasing-subsequence/)
* Try to improve to $O(N\log N)$.
* [Sort It Out (USACO Platinum)](http://www.usaco.org/index.php?page=viewproblem2&cpid=865)
* Challenging!
## Additional USACO Problems
* Fairly Straightforward
* [Hoof Paper Scissors](http://www.usaco.org/index.php?page=viewproblem2&cpid=694)
* [Cow Checklist](http://www.usaco.org/index.php?page=viewproblem2&cpid=670)
* `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)$
* Trickier
* [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`
* [Snakes](http://www.usaco.org/index.php?page=viewproblem2&cpid=945)
* `dp[first m groups][k changes] -> total sum of net sizes`
* [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`
* [Stamp Painting](http://www.usaco.org/index.php?page=viewproblem2&cpid=791)
* must be $K$ consecutive with same color
* $O(NK)\to O(N)$
* [Circular Barn Revisited](http://www.usaco.org/index.php?page=viewproblem2&cpid=622)
* can brute force make your DP easier? (yes)
* [Radio Contact](http://www.usaco.org/index.php?page=viewproblem2&cpid=598)
* [248](http://www.usaco.org/index.php?page=viewproblem2&cpid=647)
* on ranges

View file

@ -0,0 +1,27 @@
---
slug: /gold/dp-trees
title: "Dynamic Programming on Trees"
author: Michael Cao
order: 8
prerequisites:
-
- Silver - Depth First Search
-
- Gold - Introduction to Dynamic Programming
---
<!-- END DESCRIPTION -->
## Tutorial
* [DP on Trees (Codeforces)](https://codeforces.com/blog/entry/20935)
* ??
## Problems
* [Subtree](https://atcoder.jp/contests/dp/tasks/dp_v)
* [Independent Set](https://atcoder.jp/contests/dp/tasks/dp_p)
* [Gold - Barn Painting](http://www.usaco.org/index.php?page=viewproblem2&cpid=766)
* Similar to Independent Set
* [Gold - Delegation](http://usaco.org/index.php?page=viewproblem2&cpid=1019)
* Good rule of solving DP problems applies here: don't just dive into trying to figure out a DP state and transitions, and make some observations about the problem first.

View file

@ -0,0 +1,28 @@
---
slug: /plat/dp-bitmasks
title: "Dynamic Programming on Bitmasks"
author: Michael Cao
order: 10
prerequisites:
-
- Bit Operations
-
- Gold - Introduction to Dynamic Programming
---
<!-- END DESCRIPTION -->
Note: Has not appeared on recent USACO.
## Tutorial
* CPH Chapter 10
* [Dynamic Programming Over Subsets (Codeforces)](https://codeforces.com/blog/entry/337)
* [Dynamic Programming and Bit Masking (HackerEarth)](https://www.hackerearth.com/practice/algorithms/dynamic-programming/bit-masking/tutorial/)
## Problems
* [Old Gold - Moovie Moving](http://www.usaco.org/index.php?page=viewproblem2&cpid=515)
* [Matching](https://atcoder.jp/contests/dp/tasks/dp_o)
* [Square Subsets](https://codeforces.com/contest/895/problem/C)
* [Guards in the Storehouse](https://codeforces.com/problemset/problem/845/F)

View file

@ -0,0 +1,16 @@
---
slug: /plat/dp-bitmasks
title: "Dynamic Programming on Ranges"
author: Michael Cao
order: 11
prerequisites:
-
- Gold - Introduction to Dynamic Programming
---
## Problems
* [Gold - 248](http://www.usaco.org/index.php?page=viewproblem2&cpid=647)
* more straightforward
* [Plat - Greedy Pie Eaters](http://www.usaco.org/index.php?page=viewproblem2&cpid=972)
* [Plat - Subsequence Reversal](http://www.usaco.org/index.php?page=viewproblem2&cpid=698)