USACO Bronze 2019 December - Cow Gymnastics
Authors: Krish Thawani, Hua Zhi Vee, Jonathan Paulson, Ben Dodge, David Li
Official Solution
Video Solution
By David Li
Video Solution Code
Implementation
C++
#include <fstream>#include <iostream>#include <vector>using std::cout;using std::endl;using std::vector;template <typename T> int index(const vector<T> &vec, const T &n) {for (int i = 0; i < vec.size(); i++) {
Java
import java.io.*;import java.util.*;public class Gymnastics {public static void main(String[] args) throws IOException {BufferedReader read = new BufferedReader(new FileReader("gymnastics.in"));StringTokenizer initial = new StringTokenizer(read.readLine());int sessionNum = Integer.parseInt(initial.nextToken());int cowNum = Integer.parseInt(initial.nextToken());int[][] sessions = new int[sessionNum][cowNum];
Python
with open("gymnastics.in") as read:session_num, cow_num = [int(i) for i in read.readline().split()]sessions = []for _ in range(session_num):sessions.append([int(c) - 1 for c in read.readline().split()])better_pairs = 0for c1 in range(cow_num):for c2 in range(cow_num):if c1 == c2:
Alternate Solution
We generate the ranking as we read the data.
We can create a 2D array of booleans which states if cow has a ranking higher than cow in any point of time. After reading every ranking, for all pairs we set .
Then we iterate though all pairs in time and check if they satisfy the condition. At least one of and must be true, and hence we only need to check if they are both true. If one of them is false, we increment our by 1. This is because if both are true, then the pair switches ranking order at some point and is not valid. If only one is true, then the pair maintained a consistent order.
Note that at least one of them must be true, since in every ranking either or .
Implementation
Time Complexity:
C++
#include <fstream>#include <iostream>#include <vector>using std::cout;using std::endl;using std::vector;int main() {std::ifstream read("gymnastics.in");
Java
import java.io.*;import java.util.*;public class Gymnastics {public static void main(String[] args) throws IOException {BufferedReader read = new BufferedReader(new FileReader("gymnastics.in"));StringTokenizer initial = new StringTokenizer(read.readLine());int sessionNum = Integer.parseInt(initial.nextToken());int cowNum = Integer.parseInt(initial.nextToken());
Python
with open("gymnastics.in") as read:session_num, cow_num = [int(i) for i in read.readline().split()]"""better[c1][c2] = Trueif c1 was better than c2 in *any* session (0-indexed)."""better = [[False for _ in range(cow_num)] for _ in range(cow_num)]for _ in range(session_num):session = [int(c) - 1 for c in read.readline().split()]for i in range(len(session)):
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!