USACO 2019 January · Bronze · all three problems

← Full January 2019 set · Official results

Source. Statements and constraints below are paraphrased from usaco.org/index.php?page=jan19results. Each problem heading links to the canonical statement on usaco.org. Code is my own reference C++ — cross-check the official editorial before trusting it under contest pressure.

Problem 1 — Shell Game

Statement

Bessie plays the shell game with Elsie. The pebble starts under shell 1. In each of N rounds Bessie swaps the contents of two shells (positions a and b), then Elsie guesses a shell g; she wins a point if her guess matches the shell that currently hides the pebble. Bessie wants to maximize Elsie's score by choosing the best initial shell for the pebble (out of {1, 2, 3}); print that maximum score.

Constraints

Key idea

The initial pebble position has only three choices. For each of the three starting positions, simulate all N swaps with a tiny array pos[1..3] that tracks where the pebble currently is, then increment a counter when the guess equals the pebble's current shell. Return the max of the three totals.

Complexity target

O(N) per start × 3 starts = O(N). Trivially fits.

Reference solution (C++)

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int N; cin >> N;
    vector<int> A(N), B(N), G(N);
    for (int i = 0; i < N; ++i) cin >> A[i] >> B[i] >> G[i];

    int best = 0;
    for (int start = 1; start <= 3; ++start) {
        // shell[i] = which "labelled marker" currently sits under shell i.
        // Marker 'start' is the pebble; we just track its position.
        int pos = start;
        int score = 0;
        for (int i = 0; i < N; ++i) {
            if (pos == A[i]) pos = B[i];
            else if (pos == B[i]) pos = A[i];
            if (G[i] == pos) ++score;
        }
        best = max(best, score);
    }
    cout << best << "\n";
}

Pitfalls

Problem 2 — Mixing Milk

Statement

There are 4 buckets with capacities ci and current amounts mi (i = 1..4). In minute t (starting from t = 1) bucket ((t−1) mod 4) + 1 pours into bucket (t mod 4) + 1: as much milk as the source has, capped by the destination's remaining capacity. Print the contents of every bucket after 100 minutes.

Constraints

Key idea

Pure simulation. Maintain volumes m[] and capacities c[]; for each step compute the transfer amount min(m[src], c[dst] − m[dst]), subtract from src, add to dst. Print after 100 iterations.

Complexity target

O(100) — constant.

Reference solution (C++)

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    const int B = 4;
    vector<long long> cap(B), m(B);
    for (int i = 0; i < B; ++i) cin >> cap[i] >> m[i];

    for (int t = 1; t <= 100; ++t) {
        int src = (t - 1) % B;
        int dst = t % B;
        long long room = cap[dst] - m[dst];
        long long pour = min(m[src], room);
        m[src] -= pour;
        m[dst] += pour;
    }
    for (int i = 0; i < B; ++i) cout << m[i] << "\n";
}

Pitfalls

Problem 3 — Sleepy Cow Sorting (Bronze)

Statement

Bessie has a permutation of cows labelled 1..N. In one operation she takes the first cow in the line and inserts it anywhere into the remaining N−1 cows. Output the minimum number of such operations to sort the line into increasing order.

Constraints

Key idea

The cows we never touch must already be in sorted order, and they must sit at the end of the array (front cows are the ones that get pulled out and re-inserted). So find the longest already-sorted suffix: walk from the right while ai < ai+1. If that suffix has length L, the answer is N − L.

Complexity target

O(N).

Reference solution (C++)

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int N; cin >> N;
    vector<int> a(N);
    for (auto& x : a) cin >> x;

    // Find longest sorted suffix.
    int i = N - 1;
    while (i > 0 && a[i - 1] < a[i]) --i;
    // Suffix is a[i..N-1], length N - i. Answer = N - (N - i) = i.
    cout << i << "\n";
}

Pitfalls

What Bronze January 2019 was actually testing