Skip to content

Commit

Permalink
Merge pull request #4979 from JoltedCowIceCream/patch-5
Browse files Browse the repository at this point in the history
Load Balancing Editorial
  • Loading branch information
SansPapyrus683 authored Dec 14, 2024
2 parents 21c2edf + d197773 commit 13d49ed
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion solutions/bronze/usaco-617.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
id: usaco-617
source: USACO Bronze 2016 February
title: Load Balancing
author: Maggie Liu, Kevin Sheng
author: Maggie Liu, Kevin Sheng, David Guo
---

[Official Analysis (Java)](http://www.usaco.org/current/data/sol_balancing_bronze_feb16.html)

<LanguageSection>
<CPPSection>

## Explanation

We solve this problem by brute forcing over all possible placements of the two fences.
Note that we only need to consider fence positions at $x + 1$ and $y + 1$ for each cow's at $(x, y)$
because these are the only positions that affect the regions without passing through a cow.
For each pair of fences, we count the number of cows in each of the four regions and update our maximum accordingly.

## Implementation

```cpp
Expand Down Expand Up @@ -98,13 +105,15 @@ public class Balancing {
}

int minImbalance = cowNum;
// Loop through each pair of fences
for (int v : vfences) {
for (int h : hfences) {
int topLeft = 0;
int topRight = 0;
int bottomLeft = 0;
int bottomRight = 0;

// Count the number of cows in each region
for (int c = 0; c < cowNum; c++) {
if (xvals[c] < v && yvals[c] > h) {
topLeft++;
Expand Down

0 comments on commit 13d49ed

Please sign in to comment.