Video Solution
By Melody Yu
Video Solution Code
Explanation
Since there are only cows, there are distinct possible orderings, which is small enough for us to try all of them and still pass the problem in time.
If we generate them in alphabetical order and find an ordering that satisfies all the given constraints, then we can stop and print out the answer right then and there.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;const int RESTRICT_LEN = 6;// list of cows, in alphabetical orderconst vector<string> COWS = {"Beatrice", "Belinda", "Bella", "Bessie","Betsy", "Blue", "Buttercup", "Sue"};vector<vector<string>> orderings;
Java
import java.io.*;import java.util.*;class Pair<T, U> {T first;U second;public Pair(T first, U second) {this.first = first;this.second = second;
Python
from typing import List# list of cows, in alphabetical orderCOWS = ["Beatrice", "Belinda", "Bella", "Bessie", "Betsy", "Blue", "Buttercup", "Sue"]orderings = []def build(ordering: List[str]):# finished building permutation
Solution With Graphs
This solution is covered in the Introduction to Graphs module.
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!