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:
C++
#include <bits/stdc++.h>using namespace std;int N, M;// {s, t, c}vector<array<int, 3>> cows;// {a, b, p, m}vector<array<int, 4>> air_conditioners;// uses[i] == true: the i-th air conditioner is usedvector<bool> uses;
Java
import java.io.*;import java.util.*;public class AirConditioningII {static int N, M;// {s, t, c}static List<int[]> cows = new ArrayList<>();// {a, b, p, m}static List<int[]> airConditioners = new ArrayList<>();// uses[i] == true: the i-th air conditioner is used
Python
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:
C++
#include <bits/stdc++.h>using namespace std;/** @return if the given AC units satisfy the constraints of the cows */bool check(vector<array<int, 3>> &cows, vector<array<int, 4>> &air_conditioners) {// iterate through all positions to check if the current subset is feasiblefor (int i = 1; i <= 100; i++) {// iterate through air conditioners to find the current cooling unitsint cooling = 0;for (int j = 0; j < air_conditioners.size(); j++) {
Java
import java.io.*;import java.util.*;public class AirCownditioning {Code Snippet: Cow and AC Class (Click to expand)static final int MAX_STALL = 100;public static void main(String[] args) throws IOException {BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
Python
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!