This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
usaco-guide/content/5_Gold/1_Gold_DP.md

125 lines
5.9 KiB
Markdown
Raw Normal View History

2020-06-04 01:42:57 +00:00
---
slug: /gold/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 05:39:49 +00:00
order: 1
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-04 01:42:57 +00:00
---
2020-06-11 00:25:30 +00:00
**Dynamic Programming (DP)** is a very important concept which emerges in the Gold division and extends to the IOI.
2020-06-04 02:09:42 +00:00
2020-06-04 05:39:49 +00:00
<!-- END DESCRIPTION -->
2020-06-03 14:17:07 +00:00
2020-06-11 01:05:14 +00:00
[[info | Contest Tip]]
2020-06-11 01:06:44 +00:00
| Usually at least one problem from every gold division contest involves some sort of DP.
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-09 16:36:07 +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.
[[info | 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)$).
2020-06-11 00:49:01 +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:25:30 +00:00
## Bounded and Unbounded Knapsack
2020-06-09 16:36:07 +00:00
* Classic
2020-06-11 00:25:30 +00:00
* [Unbounded Knapsack](https://www.hackerrank.com/challenges/unbounded-knapsack/problem)
2020-06-09 16:36:07 +00:00
* [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)
2020-06-03 21:42:26 +00:00
* [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)
2020-06-04 22:17:28 +00:00
* Reconsider the state.
2020-06-09 16:36:07 +00:00
* USACO Gold
* [Fruit Feast](http://www.usaco.org/index.php?page=viewproblem2&cpid=574)
2020-06-11 00:25:30 +00:00
* straightforward
* [Talent Show](http://www.usaco.org/index.php?page=viewproblem2&cpid=839)
* binary search + knapsack on weight
2020-06-09 17:56:46 +00:00
* [Cow Poetry](http://usaco.org/index.php?page=viewproblem2&cpid=897)
2020-06-11 00:25:30 +00:00
* First consider the case where there are only two lines with the same class.
* Requires fast modular exponentiation for full credit.
2020-06-11 00:49:01 +00:00
* [Exercise](http://www.usaco.org/index.php?page=viewproblem2&cpid=1043)
* With a bit of number theory
2020-06-11 00:25:30 +00:00
* 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)
* 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)
2020-06-11 00:25:30 +00:00
* 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-09 16:36:07 +00:00
* [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)
2020-06-11 01:05:14 +00:00
* Challenging!