USACO 2017 December Contest Bronze Division - Milk Measurement#
Problem link: here
Solution Author: Stefan Dascalescu
Problem Solution#
Simulate this using brute force and check the changes every time.
Source codes#
The source codes in C++ and Python can be seen below.
#include <bits/stdc++.h>
using namespace std;
int qt[3], delta[3][102];
int main()
{
ifstream cin("measurement.in");
ofstream cout("measurement.out");
int n;
cin >> n;
int msk = 7; // bitmask with all cows
for(int i = 1; i <= n; i++)
{
int d, cnt;
string who;
cin >> d >> who >> cnt;
if(who[0] == 'B')
delta[0][d] += cnt;
if(who[0] == 'E')
delta[1][d] += cnt;
if(who[0] == 'M')
delta[2][d] += cnt;
}
int ans = 0;
for(int i = 1; i <= 100; i++)
{
for(int j = 0; j <= 2; j++)
qt[j] += delta[j][i];
int mx2 = max(qt[0], max(qt[1], qt[2]));
int msk2 = 4 * (qt[0] == mx2) + 2 * (qt[1] == mx2) + (qt[2] == mx2);
ans += (msk != msk2);
msk = msk2;
}
cout << ans;
return 0;
}
with open("measurement.in", "r") as fin:
n = int(fin.readline().strip())
delta = [[0] * 102 for _ in range(3)]
qt = [0, 0, 0]
msk = 7 # bitmask with all cows
for _ in range(n):
parts = fin.readline().split()
d = int(parts[0])
who = parts[1]
cnt = int(parts[2])
if who[0] == 'B':
delta[0][d] += cnt
if who[0] == 'E':
delta[1][d] += cnt
if who[0] == 'M':
delta[2][d] += cnt
ans = 0
for i in range(1, 101):
for j in range(3):
qt[j] += delta[j][i]
mx2 = max(qt[0], qt[1], qt[2])
msk2 = (4 if qt[0] == mx2 else 0) + (2 if qt[1] == mx2 else 0) + (1 if qt[2] == mx2 else 0)
if msk != msk2:
ans += 1
msk = msk2
with open("measurement.out", "w") as fout:
fout.write(str(ans))