Explanation
Since is given, we can iterate through all possible subsets of air conditioners. For each of them, we check if this subset of air conditioners satisfy the requirements of the cows by iterating through all possible positions . For each position , we can iterate through all available air conditioners in the current subset to find out the temperature reduced at and iterate through all cows to find the cow at this position and its demand. If the requirement at each position is fulfilled, we update the minimal cost accordingly.
We can generate subsets with recursion or bitmasks: both ways are presented below.
Solution 1: Subset with Recursion
Time Complexity:
cows = [] # List to store {s, t, c} for each cowair_conditioners = [] # List to store {a, b, p, m} for each air conditioner# the minimum amount of money needed to keep all cows comfortablemin_cost = float("inf")def update():"""Based on 'uses', determine if the current subset of air conditioners suffices
Solution 2: Subset with Bitmask
Time Complexity:
from typing import NamedTupleMAX_STALL = 100class Cow(NamedTuple):start: intend: intcool_req: int
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!