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/DP.md
Benjamin Qi af73a84163 lots
2020-06-22 22:17:59 -04:00

6 KiB

id title author prerequisites description
dp Introduction to Dynamic Programming Michael Cao
Recursion
Silver - Prefix Sums
Speeding up naive solutions with memoization.

This is a very important concept which emerges in the Gold division and extends to the IOI. (improve?)

Usually, at least one problem from every gold division contest involves some sort of DP.

Tutorial

The following tutorials serve as an introduction into the mindset of DP.

  • 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

The following USACO problems don't fall into any of the categories below. Arranged roughly in order of difficulty.

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)$).

  • 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)
  • Snakes
    • dp[first m groups][k changes] -> total sum of net sizes
    • O(N^4)\to O(N^3)
  • Circular Barn Revisited
    • can brute force make your DP easier? (yes)
  • Taming the Herd
    • dp[consider first i entries only][last breakout in first i occurs at j][k breakouts among first i entries] -> # changes
  • Mortal Cowmbat
    • Use Floyd-Warshall, Prefix Sums
    • dp[first i letters form valid combo][last letter] -> time
  • Stamp Painting
    • must be K consecutive with same color
    • O(NK)\to O(N)

Bounded and Unbounded Knapsack

Longest Increasing Subsequence

(add?)

Other