The Skyline Problem

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).

Buildings Skyline Contour

The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.

For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .

The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.

For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].

Notes:

  • The number of buildings in any input list is guaranteed to be in the range [0, 10000].
  • The input list is already sorted in ascending order by the left x position Li.
  • The output list must be sorted by the x position.
  • There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]

Credits:
Special thanks to @stellari for adding this problem, creating these two awesome images and all test cases.

使用扫描线法进行处理。左边点标为进入,右边点标为离开,实时维护“活动楼列表”。将同一横坐标的进入点排在前边,离开点排在后边。首先判断x点处的进入点的最高值,并将这些点加入“活动楼列表”,然后判断离开点的最高值,同时将这些点从“活动楼列表”中删除,若最高值等于当前的高度,则输出当前“活动楼列表”的最大高度。

PS:使用set或multiset维护活动楼列表时,当删除某一离开点高度时,会将该高度的所有相同值都删除,造成“活动楼列表“高度为0,产生错误,因此需在输入数据时维护一个高度列表,并在”活动楼列表“中记录进入点的高度在高度列表的坐标,避免同时删除相同高度的点。

 1 class Solution {
 2 private:
 3     #define LEFT 0;
 4     #define RIGHT 1;
 5     struct xEVENT
 6     {
 7         int x;
 8         int height_index;
 9         int side;
10         xEVENT(int _x,int _height, int _side): x(_x),height_index(_height),side(_side){}
11     };
12 private:
13 static    bool compareevent(const xEVENT& e1,const xEVENT& e2)
14     {
15         if(e1.x!=e2.x)
16             return e1.x<e2.x;
17         return e1.side<e2.side;
18     }
19 public:
20     vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
21         int n=buildings.size();
22         
23         vector<pair<int,int>> res;
24         if(n<1) 
25             return res;
26         vector<xEVENT> event;
27         vector<int> buildingheight;
28         set<int> activebuilding;
29         activebuilding.insert(0);
30         
31         for(int i=0;i<buildings.size();i++)
32         {
33             auto &b=buildings[i];
34             int index=buildingheight.size();
35             event.push_back(xEVENT(b[0],index,0));
36            
37             event.push_back(xEVENT(b[1],index,1));
38             buildingheight.push_back(b[2]);
39         }
40         sort(event.begin(),event.end(),compareevent);
41         int curheight=0;
42         pair<int,int> tmp_pair;
43         for(int i=0;i<event.size();i++)
44         {
45             if(event[i].side==0)
46             {
47                 activebuilding.insert(event[i].height_index);
48                 int newheight=buildingheight[event[i].height_index];
49                 int newx=event[i].x;
50                 while(i+1<event.size()&&event[i+1].x==newx&&event[i+1].side==0)
51                 {
52                     i++;
53                     activebuilding.insert(event[i].height_index);
54                     newheight=max(newheight,buildingheight[event[i].height_index]);
55                 }
56                 if(newheight>curheight)
57                 {
58                     res.push_back(tmp_pair=make_pair(newx,newheight));
59                     curheight=newheight;
60                 }
61             }
62             else
63             {
64                 activebuilding.erase(event[i].height_index);
65                 int newheight=buildingheight[event[i].height_index];
66                 int newx=event[i].x;
67                 while(i+1<event.size()&&event[i+1].x==event[i].x&&event[i+1].side==1)
68                 {
69                     i++;
70                     activebuilding.erase(event[i].height_index);
71                     newheight=max(newheight,buildingheight[event[i].height_index]);
72                 }
73                 if(newheight==curheight)
74                 {
75                     int maxheight=0;
76                     multiset<int>:: iterator it=activebuilding.begin();
77                     for(;it!=activebuilding.end();it++)
78                     {
79                         maxheight=max(maxheight,buildingheight[*it]);
80                     }
81                     if(maxheight<newheight)
82                     {
83                         res.push_back(tmp_pair=make_pair(newx,maxheight));
84                         curheight=maxheight;
85                     }
86                 }
87             }
88         }
89         return res;
90     }
91 };
原文地址:https://www.cnblogs.com/zl1991/p/4684818.html