d026: Sunset Posters

1 / 12

# d026: Sunset Posters

## d026: Sunset Posters

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. d026: Sunset Posters Po-Lung Chen Team Dont Block Me, National Taiwan University March 26, 2010

2. Problem Description (1/2) • Given a skyline of houses on a street. • We want to stick up some identical rectangular posters with width and height onto the wall. • The posters should touch the ground, and none of them overlaps. P P P

3. Problem Description (2/2) • How many posters can you stick on the wall? • The street length . • Number of poster queries . P P 3 5 P P P P P P P P

4. Naïve Approach (1/2) • For each poster size , we just how many consecutive heights are not less then . • For example, now , and the heights are: • Then the maximum number of posters that we can put onto the wall is . 3, 3, 5, 5, 2, 2, 3, 6, 6, 7, 4, 4, 2, 4, 5, 5, 1, 1. Length = 4 Length = 6 Length = 3

5. Naïve Approach (2/2) • We need to scan every height per query. • Total time complexity: , too slow! • What quantity is critical to each query? • For each query height, we need to know: • consecutive heights which is not less than the query height. • Notice that for , consecutive heights not less than is contained in consecutive heights . Must consider all queries together.

6. Key Observation • We glue the buildings with height not less than a certain height , result in some segments of consecutive heights not less than . • The answer is determined by lengths of the segments. • We can count the segments with same length together. • Ex: If we have 3 segments with length 17, andthen we can post posters on it. There are at most different lengths of segments at a certain height!

7. Consecutive Heights • Imagine the sea level got lower and lower, and the top of some buildings showed up one by one.

8. Combine Segments (1/2) • When a building shows up, it will combine the neighboring segments. • Each combine operation affects at most 3 segments.

9. Combine Segments (2/2) • If we sweep from tallest building to the shortest, the total number of combine operations is exactly times. • If we can keep the segments efficiently, for each query we just consider every segments of different lengths. • We can use disjoint sets to handle these segments. • Each segment is a set of buildings. • Each combine operation is a “union” of at most 3 sets. • For more information, please see:http://en.wikipedia.org/wiki/Disjoint_sets

10. The Algorithm • Sort the buildings and queries by their height. • Sweep from the highest to the lowest, • If encounters a building, union it to the neighbors, and keep a counter for different lengths of the segments. • Using an array store the number of segment lengths. • These number must be non-zero that ensures . • If encounters a query, just scan over the array to get the answer for this query. • Output the answers according to their input order.

11. Time Complexity • Sorting costs . • The total combine operation costs due to disjoint sets. • For each query, it scans all segments and get the answer in time . • Therefore the total time complexity is:

12. Finally… Thanks for your attention!