USACO Gold 2016 December - Cow Checklist

Authors: Maggie Liu, Ryan Chou


Official Analysis (Java)

Note that at any single point in time Farmer John will be at either a Holstein or a Guernsey, and will have the choice to either go to the next Holstein or Guernsey.

This suggests a DP. Let dpi,j\texttt{dp}_{i,j} represent the minimum cumulative distance for Farmer John to visit the first ii Holsteins and the first jj Guernseys.

Then, Farmer John can either go to the next Holstein or the next Guernsey.

dpi,j=min(dpi1,j,dpi,j1)\texttt{dp}_{i, j} = \min(\texttt{dp}_{i - 1,j}, \texttt{dp}_{i,j - 1})

Implementation

Time Complexity: O(HG)\mathcal{O}(H \cdot G)

C++

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const ll MAX_D = 1e18;
struct Coord {
public:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!