USACO Gold 2017 February - Why Did the Cow Cross The Road II
Authors: Brad Ma, Jesse Choe
Explanation
Let's define as the maximum number of disjoint friendly crosswalks up to field on the first side of the road and field on the second side of the road. We can build a crosswalk between fields and as long as there isn't a crosswalk between fields , and , . Thus, , where and represent the breed IDs of the cows in the -th and -th fields, respectively.
The answer is .
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;const int MAXN = 1000;int dp[MAXN + 1][MAXN + 1];int main() {freopen("nocross.in", "r", stdin);freopen("nocross.out", "w", stdout);
Java
import java.io.*;import java.util.*;public class NoCross {static int[] firstSide;static int[] secondSide;static int[][] memoization;static boolean friendly(int cow1, int cow2) {if (Math.abs(firstSide[cow1] - secondSide[cow2]) <= 4) { return true; }
Python
with open("nocross.in") as read:n = int(read.readline().strip())"""id1 represents the breed ID of the fieldson the first side of the roadid2 represents the breed ID of the fieldson the other side of the road."""id1 = [0]
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!