Skip to content

USACO 2018 December Contest Bronze Division - Mixing Milk#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

We have at most 100 moves, so we can simulate the process and keep track of how much milk we have by the time the process ends.

Source codes#

The source codes in C++ and Python can be seen below.

#include <bits/stdc++.h>
using namespace std;
ifstream f("mixmilk.in");
ofstream g("mixmilk.out");
int v[5], cap[5];
int main()
{
    for(int i = 1; i <= 3; ++i)
        f >> cap[i] >> v[i];
    for(int i = 1; i <= 100; ++i)
    {
        if(i % 3 != 0)
        {
            int x = (i % 3);
            int aa = min(v[x], cap[x+1] - v[x+1]);
            v[x] -= aa;
            v[x+1] += aa;
        }
        else
        {
            int aa = min(v[3], cap[1] - v[1]);
            v[3] -= aa;
            v[1] += aa;
        }
    }
    for(int i = 1; i <= 3; ++i)
        g << v[i] << '\n';
    return 0;
}
with open("mixmilk.in") as fin:
    tokens = fin.read().split()

cap = [0] * 4
v = [0] * 4
for i in range(1, 4):
    cap[i] = int(tokens[2*(i-1)])
    v[i] = int(tokens[2*(i-1)+1])

for i in range(1, 101):
    if i % 3 != 0:
        x = i % 3
        aa = min(v[x], cap[x+1] - v[x+1])
        v[x] -= aa
        v[x+1] += aa
    else:
        aa = min(v[3], cap[1] - v[1])
        v[3] -= aa
        v[1] += aa

with open("mixmilk.out", "w") as fout:
    for i in range(1, 4):
        fout.write(str(v[i]) + "\n")