Analysis and Solution for Jan. 2016 USACO Bronze Problem 1

Promotion Counting

Link to problem statement

Analysis

If a cow was promoted from Gold to Platinum, it could have not been in Platinum at the start. Therefore, the number of cows promoted from Gold to Platinum is just the difference of the number of cows in Platinum before the contest and the number of cows in Platinum after the contest.

Using the same logic, if the cow was promoted from Silver to Gold, it could have not been in Gold or Platinum. Therefore, the number of cows promoted from Silver to Gold is the difference of the number of cows in Gold and Platinum before the contest and the number of cows in those divisions after the contest.

The last case is if the cow was promoted from Bronze to Silver. If it was promoted from Bronze to Silver, the cow could not have been in Silver, Gold, or Platinum. This means that the number of cows promoted from Bronze to Silver is the difference of the number of cows in Silver, Gold, and Platinum before the contest and the number of cows in those divisions after the contest.

Implementations

C++17

/* USACO January 2016 Bronze 1
 * C++17 code
 */
#include <iostream>
#include <fstream>
using namespace std;
int main(){
    ifstream input("promote.in");
    ofstream output("promote.out");
    int count[4][2];
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 2; j++)
        input >> count[i][j];
    }
    input.close();
    int BS = count[1][1] + count[2][1] + count[3][1] - count[1][0] - count[2][0] - count[3][0];
    int SG = count[2][1] + count[3][1] - count[2][0] - count[3][0];
    int GP = count[3][1] - count[3][0];
    output << BS << "\n" << SG << "\n" << GP << "\n";
    output.close();
    return 0;
}

Java 8

/* USACO January 2016 Bronze 1
 * Java 8 code
 */
import java.io.*;
import java.util.*;
public class Jan2016Bronze1{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("promote.in"));
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("promote.out")));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int[][] count = new int[4][2];
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 2; j++)
                count[i][j] = Integer.parseInt(st.nextToken());
            if (i != 3)
                st = new StringTokenizer(br.readLine());
        }
        br.close();
        int BS = count[1][1] + count[2][1] + count[3][1] - count[1][0] - count[2][0] - count[3][0];
        int SG = count[2][1] + count[3][1] - count[2][0] - count[3][0];
        int GP = count[3][1] - count[3][0];
        pw.println(BS);
        pw.println(SG);
        pw.println(GP);
        pw.close();
    }
}
Written on June 17, 2018
Back