Videos

Video

Mixing Milk

Read the guide

Use the source amount and destination space to decide how much to pour, then update both buckets.

Watch on YouTube

Capacities stay fixed. The milk amounts change at every step. Pour the smaller of the milk in the source and the free space in the destination.

Trace the first cycle

The capacities are 10, 11 and 12. The starting amounts are 3, 4 and 5. The cycle is 1→2, 2→3, 3→1.

Pour Amount moved Amounts afterwards
1→2 min(3, 11 − 4) = 3 [0, 7, 5]
2→3 min(7, 12 − 5) = 7 [0, 0, 12]
3→1 min(12, 10 − 0) = 10 [10, 0, 2]

The second pour uses the updated 7, and the third uses the updated 12. After every pour, the total stays at 12 and no bucket exceeds its capacity.

Run exactly 100 pours

For a zero-based step t, use t % 3 for the source and (t + 1) % 3 for the destination.

def mix(capacity, initial):
    milk = initial.copy()
    for t in range(100):
        src = t % 3
        dst = (t + 1) % 3
        amount = min(milk[src], capacity[dst] - milk[dst])
        milk[src] -= amount
        milk[dst] += amount
    return milk

Test the last pour and a full destination

  • The official sample ends at [0, 10, 2]. Thirty-three full cycles only account for 99 pours.
  • If all buckets start full, with capacities and amounts both [3, 4, 5], every pour moves zero and the amounts stay the same.

This historical problem reads mixmilk.in and writes one result per line to mixmilk.out.

Official USACO problem and sample · count the minimum changes with a frequency table.