USACO Silver 2016 February - Load Balancing

Author: Benjamin Qi

Official Analysis (Java)

Solution 1

Similar to the analysis.

Time Complexity: O(N2)\mathcal{O}(N^2)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!
using pi = pair<int,int>;
using pl = pair<ll,ll>;

Solution 2

Time Complexity: O(N3)\mathcal{O}(N^3)

It does not suffice to check only a constant number of xx and yy-coordinates that are close to the median xx or yy, respectively. For example, consider the case where N=999N=999 and xi=yi=2i1x_i=y_i=2i-1. Then M=333M=333.

However, we can speed up an O(N3)\mathcal{O}(N^3) solution by a constant factor by checking fewer xx and yy coordinates than the bronze editorial does. Note that if we increase the xx-coordinate of the north-south fence by two and there are still at most N3\frac{N}{3} cows to the left of the fence, then the answer for this new configuration cannot be worse. Thus, the number of xx-coordinates which the solution below checks is at most N3\frac{N}{3} (plus a constant). Similar reasoning holds for the yy-coordinates.

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!
using pi = pair<int,int>;
using pl = pair<ll,ll>;

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!