Load Balancing solution report (USACO 2016 February Contest, Silver. Problem 2. sorting tree array discretization)

USACO (W) (funny)

Topic analysis

Topic online details (submitted address): Http://www.usaco.org/index.php? Page=viewproblem2&cpid=619

The meaning of the topic is very simple. Given a two-dimensional matrix (farm), there are many points (cattle) placed in it, so that we can cut the horizontal and vertical two knives in a suitable place, so that the points in the four regions with the middle point are the least. (what, not yet understood? Go back to the subject details and continue to bite English.

Algorithm design

Without considering the special advanced algorithm of k-d tree, let's first think about how to do violence. Enumerate each cutting point (the intersection point of horizontal vertical cut line). However, we can find that because X and y are very large, the final time of the program will be very slow.

Wait! Why are they more than 1000? Yes, we can simply think in the brain, such a large matrix, a lot of space is 0, that is, we enumerated a lot of useless location points, we can x, y alone (think why do it alone can be done) to make a discretization, so that the scope of enumeration can be reduced quickly. A step closer to success!

However, it is still very slow O (n^3), the reason is actually very simple, because the points in the statistical area are very slow, every statistic we have counted the last statistics. Then why don't we think of a way to solve this problem? First of all, we can make these cows. Orderly (actually, this is my initial idea, earlier than the discretization idea). We pushed forward according to the line, then we set up a tree array on the upper and lower level of the horizontal cutting line. Actually, at first, my idea was to set up a line tree for every line, but found that there was no need to build one on every line. Then enumerated each vertical cut line, so that our complexity reduced to O (n ^ 2 * logn).

Example code

`````` #include <cstdio>
#include <cstring>
#include <algorithm>

const int N = 1100;

int n;
struct CD {
int {
int, *;}}; D (int *a, int *b) {
return *a < *b;
}

int t[N], b[N]; / / horizontal axis cut the upper and the lower two parts, the
}

int (0), the + + = =; (+) = + (=); (0) + + =;
}

int main () {/ / freopen (" Input.txt "", "R", "stdin";
freopen (")". Balancing.in "", "R", "stdin";
freopen (")". Balancing.out "," W "," stdout ";

scanf ("%d ", &n);
for (int i = 1; I = = n; i++) scanf (" "," ","); "t" / / "sort" to facilitate the lower horizontal axis of movement: ((+ + 1, + + + 1)); / / / () = (1); I++) printf ("%d%d\n", c[i].x, c[i].y); printf ("\n"); and%d / / y are discretized so as to facilitate the downward movement of the vertical axis
for (int = 1; < 1); = =; (2): (+ +) + + + + +. D);
int last = 0, count = 0;
for (int = I = 1; I < = n; i++) {
if (i++ = =);; = =;; = =;; =} / / / ((= = 1); < (=); (); "(", ");" (",", ");" (","); "(") ";" ("")); "/" / "mobile" horizontal axis "; and enumerating the vertical axis (", 0, ");
memset (B, 0, sizeof b);
for (int = I = 1; I < = n; i++) change (n, i++, 1); / / first fill the content below the horizontal axis; = = 1; C[i].y, 1);
change (B, c[i].y, -1);
i++;
}

for (int j = 1; J = = n; n) Ans = 0;
tmp = sum (T, J); / / upper left
if (TMP > stage_) ANS) stage_ Ans = TMP;
tmp = sum (T, n) - TMP; / / upper right
if (TMP > stage_) ANS) stage_ Ans = TMP;
tmp = sum (B, J); / / lower left
if (TMP > stage_) ANS) stage_ Ans = TMP;
tmp = sum (B, n) - TMP; / / lower right
if (TMP > stage_) ANS) stage_ Ans = TMP;
if (stage_) Ans < ANS) ans = stage_ Ans;
}
}
printf ("%d", ANS);

return 0;}``````