EXERCISES AND PROBLEMS: Try to solve the Skyline problem Submitted by Ray Polivka Suppose you are given a series of rectangular boxes laid along the positive x axis. Each box is defined by 3 integer numbers; a left x value, a height along the y axis, and a right x value. For example one triple might be (1 11 5), meaning that the box extends horizontally from x=1 to x=5, with a height on the y axis of 11 units. The rectangular boxes may overlap or intersect as well as stand alone. For example, the three boxes represented by (1 11 5), (2 6 7) and (3 13 9) represent two intersecting boxes, (1 11 5) (3 13 9) and one completely subsumed one (2 6 7). You are expected to create a function called Skyline, which will produce the outline of the boxes, eliminating hidden boxes and intersections. The result should be, essentially, a skyline. The result should be a simple vector consisting of the left-most starting x value, followed by the associated height, followed in turn by the x value, with its associated height. This continues alternating x values and y heights until the sky outline is completed. You may assume that the left x values (the starting values for each box in its triplet) are given in ascending order. Test Case (see Figure 1): Let an LHR vector be (1 11 5) (2 6 7) (3 13 9) (12 7 16) (14 3 25) (19 18 22) The skyline result should be: (1 11 3 13 9 0 12 7 16 3 19 18 22 3 25) Note: This problem has been modeled after problem 105 in Vol. 1, from the problem set archive of the Universidad de Valladolid at the following web url: http://acm.uva.es/problemset Problems in this library are collected from different local, regional and final ACM contests. Problem 105 comes originally from Internet Duke. Solutions, stating clearly which language and version has been used, should be sent to: polivka@acm.org In future issues of APL Quote Quad, selected correct solutions submitted and the names of their authors will be published. y Figure 1: the problem to be solved x September 2004, Volume 34, Number 4
/lp/association-for-computing-machinery/exercises-and-problems-try-to-solve-the-skyline-problem-cPetmyJ8Ya