270 likes | 379 Views
Storage Centre. HKOI 2007 Senior Q1 Kelly Choi. The Problem. The COW Team are going to look for treasures in N ruins in a rectangular map. A storage centre is to be built in one of the regions (possibly one containing a ruin).
E N D
Storage Centre HKOI 2007 Senior Q1 Kelly Choi
The Problem • The COW Team are going to look for treasures in N ruins in a rectangular map. • A storage centre is to be built in one of the regions (possibly one containing a ruin). • We would like to minimize the maximum of the Manhattandistance from the centre to any ruin. • Manhattan distance from (x1, y1) to (x2, y2)= |x1-x2| + |y1-y2|
Without the story… • Given N points (xi,yi) with integral coordinates, • Choose a centre (x,y) on the coordinate plane: • with integral coordinates. • such that max{|x – xi| + |y – yi|} is minimized among all possible centres.
Observation 1 • The centre will not lie outside the smallest rectangle (with sides parallel to the x and y-axes) enclosing all the N points.
Algorithm 1 • Due to this observation, we can try every point within the rectangular region R and find out the minimized maximum distance. min ←∞ For each point (x, y) in R If max {|x-xi| + |y-yi|} < min then min ← max {|x-xi| + |y-yi|} store x and y
Algorithm 1 • Algorithm 1 has a time complexity of O(XYN), • where X = max{xi} – min{xi}, Y = max{yi} – min{yi} • It can pass 50% of the test case. But it is still too slow.
Let’s try another way… • Intuition suggests that we are looking for some kind of “centre”. But what kind of centre are we looking for?
Thinking… • If • direct distance instead of Manhattan distance • Coordinates of centre can be decimal numbers • Then…?
Coverage of the centre • The idea of the circular coverage in the previous case naturally leads us to think about the coverage of the storage centre, given its maximum distance
Transformation • Now the problem can be rewritten as follows: • Given N selected regions • Find the smallest maximum distance such the coverage of the storage centre can cover all points • This is equivalent to trying to fit the coverage on the points, increasing its size whenever failed. • Now, try to do it a few times by hand.
Observation 2 • After trying to fit the shape on the points a few times, you should realize that the task depends on the 45º ‘lines’ instead of lines parallel to the axes. • Points lying on the same 45º lines have the same (x + y) or (x – y) values.
Observation 2 • In the right figure, you will not cover more points if you situate the coverage figure beyond the black line.
Observation 2 • However, the black lines do not always outline the shape of the coverage. • So we have to make decisions.
Case 1 • The black lines exactly form a shape of the coverage
Case 2 • The coverage may be larger than the area enclosed by the black lines.
Case 3 • Things may not be that symmetric.
Lastly, mathematics • The black lines can be represented by its (x + y) or (x – y) values. • A coverage figure can be represented by either: • Its four edges; or • The centre and the size • With the black lines, we can directly find out the four edges of the coverage figure, and thus the centre.
An Example of Calculation • Black lines: • x + y = 6, x + y = 14 • x – y = -4, x – y = 0 • Coverage figure: • x + y = 6, x + y = 14 • x – y = -4, x – y = 4 • Center: • Solving • x + y = (6 + 14)/2 = 10 • x – y = (-4 + 4)/2 = 0 • Gives (x,y) = (5,5)
Calculations • Given the (x + y) and (x – y) values of the black lines, we can condition on which case it is and find out the coverage figure, and then the centre. • The ideas we have dealt with may be a bit complicated, but the calculations are simple. • Finding out the edges of the coverage figure first avoids the problem in directly rounding off the values from the black lines.
Comments • Interesting problem • Requires observation • You can improve yourself by thinking about the problem in different approaches. • Requires some simple yet not so straight-forward mathematics • May require some ability to visualize the problem too
Contestants’ performance • Many contestants aimed at 50% of the test cases • That solution is pretty easy to arrive • Careless mistakes (overflow, missing initialization) and negligence in rounding off • Boundary cases: N = 1 • Lack of time / thinking
Further thinking • Can the problem be done in other ways? • Can the solution be modified to solve a 3D Storage Centre problem? • Can it be generalized to n-dimensional? • How to solve the problem when direct distance is used instead of Manhattan distance? • What is the difference in restricting the coordinates of the centre to be integers or not?