Skip to content

Possible discrepancy in "Milk Measurement" editorial / solution (Test case outputs 6 instead of 5) #205

@Aditya6582

Description

@Aditya6582

Hi, I was working on the USACO Bronze problem Milk Measurement and noticed a potential discrepancy between the editorial solution (or accepted code) and my manual dry run for the following test case:

Input:

5 10
1 1 +5
2 1 -5
3 1 -5
4 2 -5
5 3 -5
6 3 +4

My Dry Run:

Initial milk output = 10 for all cows.

Day 0: 10, 10, 10, 10, 10 → displays: 0

Day 1: 15, 10, 10, 10, 10 → displays: 1

Day 2: 10, 10, 10, 10, 10 → displays: 2

Day 3: 5, 10, 10, 10, 10 → displays: 3

Day 4: 5, 5, 10, 10, 10 → displays: 4

Day 5: 5, 5, 5, 10, 10 → displays: 5

Day 6: 9, 5, 5, 10, 10 → displays: still {3rd cow with 10}, so answer should remain 5

So according to this, the display changes 5 times.

Accepted Solution Output

However, when I run the editorial/guide solution (code below), the output is 6.

Code:

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define endl '\n'
#define int long long
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

template
using ordered_set = tree<T, null_type, less, rb_tree_tag, tree_order_statistics_node_update>;

const int MOD = 1e9 + 7;
const int N = 1e6 + 2;

void solve() {
freopen("measurement.in", "r", stdin);
freopen("measurement.out", "w", stdout);
int n, g; cin >> n >> g;
vector<array<int, 3>> v(n);
map<int, int> mp;
for (int i = 0; i < n; ++i) {
cin >> v[i][0] >> v[i][1] >> v[i][2];
mp[v[i][1]] = g;
}
sort(v.begin(), v.end());

map<int, int> count;
count[g] = (int)mp.size();

int cnt = 0;
for (auto &[day, id, milk] : v) {
    int oldval = mp[id];
    int old_max = count.rbegin()->first;
    bool was_top = (oldval == old_max);
    int prev_count = count[oldval];

    count[oldval]--;
    if (count[oldval] == 0) count.erase(oldval);

    int newval = oldval + milk;
    mp[id] = newval;
    count[newval]++;

    int new_max = count.rbegin()->first;
    bool is_top = (newval == new_max);
    int curr_count = count[newval];

    if (was_top) {
        if (!is_top || curr_count != prev_count) cnt++;
    } else {
        if (is_top) cnt++;
    }
}
cout << cnt << endl;

}

signed main() {
auto begin = chrono::high_resolution_clock::now();
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int t = 1;
while (t--) solve();

auto end = chrono::high_resolution_clock::now();
auto elapsed = chrono::duration_cast<chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;

}

Question

Is this a mistake in my reasoning, or does the official code count an extra change on day 6 even though the set of top cows (with 10) remains unchanged?

If the code is correct, could you please clarify why the extra increment happens here? It would help clear confusion for readers.

Environment

Problem: USACO Silver – Milk Measurement

Source: USACO Guide (Accepted Code)

Compiler: g++17

Suggested improvement: maybe add a clarification/example in the editorial showing this exact edge case, so learners can understand why the answer is 6 instead of 5.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions