In the Mid-Atlantic Regional ACM Programming Contest this year, one of the more interesting problems was the following:
Suppose there are N≤105 stars such that star i has coordinates xi,yi,zi, where |xi|,|yi|,|zi|≤109. Given K≤109, count the number of pairs of stars which are within distance K from each other. The cluster of stars is sparse -- that is, there are at most M=105 pairs of stars within K of each other.
One simple solution is to think about partitioning the space up into cubes of side length K. Then, for each star, it suffices to check the number of stars in the same cube and adjacent cubes and see whether they're within K. Surprisingly enough, this actually runs in O(M) operations.
First we consider the case where space is one-dimensional, and our cubes are just intervals of length K. Let the intervals contain a1,a2,…,an elements. Note that two stars within the same interval are guaranteed to be within K of each other. Thus, n∑i=1a2i≤M
The same argument can be extended to cubes -- however, in order to guarantee that ∑ni=1a2i≤M, we need to make our cubes be of size K/√3 so that all points within them are guaranteed to be at most K from each other. This implies that if we instead use cubes of size K, then ∑ni=1a2i≤33M. We can then apply the rearrangement argument as before, except with 27 adjacent cubes instead of 3. This gives us an algorithm which runs in 729M.
In general, for dimension D we'll need (3D)DM operations for the same algorithm, so in high dimensions we'd likely need to exploit other tricks, like the fact that the volume of an n-ball disappears as n increases. We could then (maybe?) approximate the sphere with small cubes of size ϵK instead of simply summing over 3D adjacent, larger cubes of size K.
* -- You could also prove this using Cauchy-Schwarz or some other famous inequality. Rearrangement just happened to be the first one I thought of.
No comments:
Post a Comment