Editorial for WC '15 Contest 4 J4 - Target Practice
Submitting an official solution before solving the problem yourself is a bannable offence.
We have  regions of a target (
 circle surrounded by 
 rings) and 
 coordinates. We want to assign 
 given point values to the 
 regions such that the total score is minimized (and then again so it's maximized). The score is defined as the sum of the products of each ring's score with the number of shots that land in each ring. Formally, the problem asks to find the minimum and maximum possible cross products between the hit-counts of each ring and the point value we assign to each ring. By definition, the cross-product of two size 
 arrays 
 and 
 is equal to the sum 
.
By the simple equation of a circle centered at the origin, we know that a shot  lands in ring 
 iff 
 is strictly less than or equal to the outer radius 
, and strictly greater than 
 (if 
). To avoid any possible precision issues with floating point numbers (in cases where a shot lands very close to the border of a ring), this process can be handled entirely using (64-bit) integers, by comparing squared distances instead (for example, comparing 
 against 
). The hit counts can be precomputed naïvely by looping through each of the 
 rings, and then counting how many of the 
 darts land in the ring using the formula above. This will have a running time of 
. Then, we can try naïvely permuting all 
 orderings of 
 and assign them to each ring, and then for each permutation computing the cross product of the current permutation and the hit-counts of each ring. Doing this should pass the first subtask in 
 time (since 
, so 
), and is thus given 6/40 points.
Suppose we already have the hit-counts for each ring. Now intuitively, in order to maximize Bond's score, we should assign the largest  values to the rings which are hit by the largest number of shots, and the smallest 
 values to those which are hit the least (and vice versa to minimize his score). This is as easy as sorting 
 and the counts in increasing order, calculating the cross-product (for the maximum), then reversing 
 and calculating the cross-products again (for the minimum). Since each sort takes 
, we've reduced the time complexity to 
. This is sufficient to pass all but the last set of cases, obtaining 17/35 points. However, to get full marks, we must handle the bottleneck of computing the hit-counts efficiently.
If we just consider the rings in terms of their distances, then we actually just get a sorted array of integers. To find where a point lands, we want to find the first  value which is not less than the distance of the shot from the origin. Since the distances are sorted, we can use binary search in 
 time to pinpoint the exact position where it lands. Across 
 shots, the time complexity will be 
. So, the overall solution will run in 
.
Comments