Explanation
After we sort the input, an important observation is that if Bessie is at position , then the number of cows lying are all the cows with position less than that says "L" plus all the cows with a position greater than that says "G".
Given this, we can loop through every cow's position and compute the total number of liars by using a forwards loop and a backwards loop.
The answer is the minimum liars over all the positions we looped over.
Implementation
Time Complexity:
from typing import NamedTupleclass Cow(NamedTuple):pos: intstatement: strn = int(input())cows = []
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!