I'm really kicking myself for deciding to try and finish C-large which
failed, I should have spent the time to read D fully, realize the D-
small is just asking to calculate the intersection of 2 circles 10,000
times, which is completely trivial - just google around for the
formula if you can't derive it yourself. Stupid, stupid, stupid - B-
complete, C-small (trivial - just emulate the entire grid) and D-small
together is more than enough to finish in the top 500. Blast. Oh well,
better luck for me next time.
In the post-mortem analysis with a friend, before we read the official
contest analysis, we came up with an interesting alternative solution
for D which seems way simpler. I was wondering if someone could test
our analysis before I try to develop it.
Here's the basic idea:
Basically, we divide up the entire field into little square boxes. Per
such box, we check if it is entirely inside of A. If so, it
contributes its entire area to A. If entirely outside of A, it
contributes 0. If the box looks like it contributes only a part of it
to A, we simply divide our box into smaller boxes and recursively
apply this algorithm until we get such tiny boxes that simply taking
half the area of the box will be fine (will not result in a total
absolute error of more than 10^-6).
There's one big intuitive leap you'd have to make which I'm not 100%
sure about, and it is this:
Given the square box drawn by [x1,y1] to [x2,y2], then this entire box
lies inside of A if points [x1,y1], [x1,y2], [x2,y1] and [x2,y2] are
ALL inside A. Similarly, the entire box lies outside of A if all those
points lie outside of A. Said differently, if you draw any rectangle
anywhere on the field, if its corners are all either all in or all
outside of A, then the entire square is either all in or all outside
of A.
This seemed to be intuitively true to me before I read the contest
analysis, but after reading it I'm more sure but still not math-savvy
enough to prove it. All this talk about convex shapes does seem to
point at that being true.
If I may assume for a moment I'm not wrong there, then this algorithm
should be fast enough:
Start out with the entire field as the original "root" box - it's 20k
by 20k points in the worst case. We grid this box up by drawing 16
points inside of it, 4 by 4, evenly spaced. We check for each such
point if that point lies inside of, or outside of, A. This is trivial
- for each point and each goat, check the point against the pole
(center of circle) and the distance from pole to bucket (radius of
circle). Checking if a point is inside a circle is easy. If the point
is outside of any 1 goat-circle, it's outside of A. If it's inside all
goat-circles, it's inside of A.
If you draw lines through our grid points you end up with 16 little
squares. For all squares where its corners all have the same state, we
can immediately calculate the total contribution to A of that box: If
all points are "out", that box contributes 0 area. If all points are
in, that box contributes (y2-y1)*(x2-x1) area. After this round we're
left with a couple of boxes that didn't have all 4 corners with the
same state.
We recursively handle each such box by splitting it up into 16 new
smaller boxes. We escape our recursion algorithm like so: If the total
area of our box is less than 10^-6, we don't recurse and instead
return totalArea/2 as approximation of the total area contributed by
this tiny box. As a safety margin we may have to go for 10^-7 or so,
but the algorithm is fast enough for some leeway here to be certain
the error will be acceptable.
Doing the math, considering that a reasonably modern computer can run
about 3 billion "does this point lie inside this circle" operations
every second, even in the worst case scenario this box-splitting
concept will easily fit:
3 billion per second, or, considering 5000 goats and assuming it's the
3000th goat every time that decides, that's 1m points that can be
checked every second. We've got 1000 buckets to check, and 10 test
cases, so that really boils down to 100 points a second, but we have
about 7:30 minutes, so that's 45000 points we can check for any single
instance of the problem (instance being defined as test case Tx with
bucket By). Each recursive grid application costs 16 points, so that
gives us 28125 squares we can split into pieces.
That feels like it should easily do.
Looking at the implementation difficulty of the algorithm itself - it
seems like it would be far, FAR simpler to implement than the rocket
science approaches that the 2 finishers have as well as what the
contest analysis suggests. The meat of it would be this simple thing:
public double r(double x1, double x2, double y1, double y2) {
double area = x2-x1 * y2-y1;
if (area < ERROR) return area / 2;
boolean nw = check(x1, y1);
boolean ne = check(x2, y1);
boolean sw = check(x1, y2);
boolean se = check(x2, y2);
if (nw&&ne&&sw&&se) return area;
if (!nw&&!ne&&!sw&&!se) return 0;
double f1 = (x2-x1)/4;
double f2 = (y2-y1)/4;
area = 0;
for (int m = 0; m < 4; m++) for (int n = 0; n < 4; n++) {
area += r(x1 + f1*m, x1 + f1*(m+1), y1 + f2*n, y1 + f2*(n+1));
}
return area;
}
Much easier to write something that takes 20 minutes to put down then
wait 6 minutes for the answer than spend over an hour designing and
building it, I say.
NB: Each edge that is shared between 2 boxes that get split is double-
calculated. This is recursive so some points get 'checked' lots of
times. it's a bit annoying to memoize 2-double-tuples, but its
probably worth doing; a single check can take 5000 steps (check
against 5000 goats), after all.
--
You received this message because you are subscribed to the Google Groups
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-code?hl=en.