Explanation
We can calculate the amount of milk produced by each cow and put all of them in a vector
in the format of a 2-tuple: (amount, cow_name).
After sorting, we can traverse the array once to see which cow produced the second-smallest amount of milk.
Warning: Weak test data
Make sure that your solution outputs Tie for both these test cases:
4 Bessie 1 Elsie 1 Daisy 2 Gertie 3
7 Bessie 1 Elsie 1 Daisy 2 Gertie 2 Annabelle 3 Maggie 4 Henrietta 4
Implementation
Time Complexity:
COW_NUM = 7with open("notlast.in") as read:raw = {}for _ in range(int(read.readline())):name, amt = read.readline().split()amt = int(amt)if name not in raw:raw[name] = 0raw[name] += amt
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!