Skip to content

Commit

Permalink
Merge pull request #5141 from letsbegincode/add-java-solution
Browse files Browse the repository at this point in the history
Add Java Solution for USACO Problem 1276 - Air Conditioning II
  • Loading branch information
SansPapyrus683 authored Feb 21, 2025
2 parents f12ed41 + d5ed9cc commit 63c466c
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions solutions/bronze/usaco-1276.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,115 @@ int main() {
```

</CPPSection>
<JavaSection>

```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
static boolean[] uses;
// the minimum amount of money needed to keep all cows comfortable
static int minCost = Integer.MAX_VALUE;

/**
* Based on 'uses', determine if the current subset of air conditioners suffices
* the constraints, and if so, update the minimum cost
*/
static void update() {
boolean isFeasible = true;

// iterate through all positions to check if the current subset is feasible
for (int i = 1; i <= 100; i++) {
// iterate through air conditioners to find the current cooling units
int cooling = 0;
for (int j = 0; j < M; j++) {
if (!uses[j]) continue;
int[] ac = airConditioners.get(j);
int a = ac[0], b = ac[1], p = ac[2];
if (a <= i && i <= b) cooling += p;
}

// iterate through cows to find the current cow
int cowRequirement = 0;
for (int j = 0; j < N; j++) {
int[] cow = cows.get(j);
int s = cow[0], t = cow[1], c = cow[2];
if (s <= i && i <= t) {
cowRequirement = c;
break;
}
}

// For each position, the requirement of the cow must be met
if (cooling < cowRequirement) {
isFeasible = false;
break;
}
}

if (isFeasible) {
int cost = 0;
for (int i = 0; i < M; i++) {
if (uses[i]) cost += airConditioners.get(i)[3];
}
minCost = Math.min(minCost, cost);
}
}

/**
* Expand the subset, represented by 'uses', by choosing to (not) use the i-th
* air conditioner
*/
static void search(int i) {
if (i == M) {
update();
} else {
uses[i] = false;
search(i + 1);
uses[i] = true;
search(i + 1);
}
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
cows.add(new int[] {s, t, c});
}

for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int p = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
airConditioners.add(new int[] {a, b, p, m});
}

uses = new boolean[M];
search(0);

System.out.println(minCost);
}
}
```

</JavaSection>
<PySection>

```py
Expand Down

0 comments on commit 63c466c

Please # to comment.