120 likes | 130 Views
Optimize the construction of an H-shaped stage that covers all hot spots in a zoo while minimizing its area, using integral coordinates and considering symmetry. Explore different solutions and their mathematical expressions.
E N D
Little Stage HKOI 2007 Junior Q2
The Problem • Given N hot spots in the zoo, build a H-shaped stage that covers all hot spots, with its area minimized. • Integral coordinates • The H-shaped stage: • Composed of one-unit wide three blocks, each parallel to the boundary of the zoo • Symmetric in shape
How can the stage be built? • Does a “|” satisfy the requirement? • Does an “I” satisfy the requirement? • Is the coordinate plane bounded?
When is it possible? • Can we always build an H-shaped stage? • Obviously not, since the width of each block is restricted to 1. • When can we build an H-shaped stage? • Can we express this in mathematical terms?
If it is possible • Will an “H” and an “I” be both possible? • To calculate its area, we can find out the blocks first. • How to find out the blocks?
Naïve Solution • For each x coordinate xl as the left block • For each x coordinate xr as the right block, xl<xr • For each y coordinate as the middle block • Check if all points lie on them • If yes, find out the minimum possible length of the left and right block, and then the area • Do the same for the other direction • O(X2YN + XY2N) • Note the possible range of x, y
Optimization • In the third for-loop, how many y values will we succeed with? • One only, or zero • Can we find out that y value more efficiently? • Yes • For that y value, all the N points either lie on the left and right block, or between the left and right block and on that y coordinate • What if all points lie on the left and right block?
Second attempt • For each x coordinate xl as the left block • For each x coordinate xr as the right block, xl<xr • Check if all points that do not lie on the left or right block have the same y coordinate • If yes, take that y coordinate for the middle block, find out the minimum possible length of the left and right block, and then the area • Do the same for the other direction • O(X2N + Y2N)
Optimization again • How about the first two for-loops? • Actually, there are only one possible case. • There must be no points on the left of the left block, nor on the right of the right block. • So xl = min {xi} and xr = max {xi}
Final attempt • Take min {xi} as the left block • Take max {xi} as the left block • Check if all points that do not lie on the left or right block have the same y coordinate • If yes, take that y coordinate for the middle block, find out the minimum possible length of the left and right block, and then the area • Do the same for the other direction • O(N)
Implementation • Be careful with: • Cases where it is impossible to build the stage • Boundary cases and special cases
What have we learnt? • Observations can help to make your program faster • Try to think of: • What is unnecessary • Other ways to do the same thing, more efficiently