USACO 2016 December · Bronze · all three problems

← Full December 2016 set · Official results

Source. Statements and constraints below are paraphrased from usaco.org/index.php?page=dec16results. 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 — Square Pasture

Statement

Farmer John has two non-overlapping axis-aligned rectangular pastures, given by their lower-left and upper-right corners. He wants to replace them with one axis-aligned square that fully contains both rectangles. Output the smallest possible area of such a square.

Constraints

Key idea

Compute the bounding box of the two rectangles: xLo = min of both x1, xHi = max of both x2, similarly for y. The smallest enclosing square has side s = max(xHi − xLo, yHi − yLo); the answer is s². No geometry tricks are needed because the input range is tiny.

Complexity target

O(1) arithmetic. Coordinates are 0–10, so even brute force over the 11×11 lattice is trivial.

Reference solution (C++)

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    // Each rectangle: x1 y1 x2 y2 (lower-left and upper-right corners).
    int ax1, ay1, ax2, ay2, bx1, by1, bx2, by2;
    cin >> ax1 >> ay1 >> ax2 >> ay2;
    cin >> bx1 >> by1 >> bx2 >> by2;

    int xLo = min(ax1, bx1), xHi = max(ax2, bx2);
    int yLo = min(ay1, by1), yHi = max(ay2, by2);

    int side = max(xHi - xLo, yHi - yLo);
    cout << side * side << "\n";
}

Pitfalls

Problem 2 — Block Game

Statement

Farmer John has N two-sided spelling boards. Each board shows a word on each side. For any board, only one side is visible. The cows need a multiset of letter blocks (each block has one letter on it) such that no matter which side of each board faces up, they can spell every visible word using a subset of their blocks. Find the minimum total number of blocks.

Constraints

Key idea

For each board the worst case is "whichever side is up, we need enough copies of every letter to spell that word". So for each board and each letter c, the requirement is max(countfront(c), countback(c)). Sum that per-board worst case across all 26 letters and across all boards — each board is independent because boards never need to be spelled simultaneously.

Complexity target

O(N · L) where L is total word length. Trivial.

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;

    // Per-letter answer = max over boards of max(front_cnt, back_cnt).
    int need[26] = {0};
    for (int i = 0; i < N; ++i) {
        string a, b; cin >> a >> b;
        int ca[26] = {0}, cb[26] = {0};
        for (char c : a) ca[c - 'a']++;
        for (char c : b) cb[c - 'a']++;
        for (int j = 0; j < 26; ++j)
            need[j] = max(need[j], max(ca[j], cb[j]));
    }
    int ans = 0;
    for (int j = 0; j < 26; ++j) ans += need[j];
    cout << ans << "\n";
}

Pitfalls

Problem 3 — The Cow-Signal

Statement

Given an M×N grid of characters and a scaling factor K, output the grid scaled up by K in both dimensions: each input cell becomes a K×K block of the same character.

Constraints

Key idea

For each input row, print K copies of that row, where each character is itself repeated K times. No data structure beyond a string is needed.

Complexity target

O(M · N · K²) output. With all parameters ≤ 10 this is at most 10000 characters.

Reference solution (C++)

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int M, N, K; cin >> M >> N >> K;
    vector<string> g(M);
    for (auto& r : g) cin >> r;

    // For each input row, emit K copies; each character expanded to K copies.
    for (int i = 0; i < M; ++i) {
        string expanded;
        expanded.reserve(N * K);
        for (char c : g[i]) expanded.append(K, c);
        for (int rep = 0; rep < K; ++rep) cout << expanded << "\n";
    }
}

Pitfalls

What Bronze December 2016 was actually testing