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).

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,产生错误,因此需在输入数据时维护一个高度列表,并在”活动楼列表“中记录进入点的高度在高度列表的坐标,避免同时删除相同高度的点。

 class Solution {
private:
#define LEFT 0;
#define RIGHT 1;
struct xEVENT
{
int x;
int height_index;
int side;
xEVENT(int _x,int _height, int _side): x(_x),height_index(_height),side(_side){}
};
private:
static bool compareevent(const xEVENT& e1,const xEVENT& e2)
{
if(e1.x!=e2.x)
return e1.x<e2.x;
return e1.side<e2.side;
}
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
int n=buildings.size(); vector<pair<int,int>> res;
if(n<)
return res;
vector<xEVENT> event;
vector<int> buildingheight;
set<int> activebuilding;
activebuilding.insert(); for(int i=;i<buildings.size();i++)
{
auto &b=buildings[i];
int index=buildingheight.size();
event.push_back(xEVENT(b[],index,)); event.push_back(xEVENT(b[],index,));
buildingheight.push_back(b[]);
}
sort(event.begin(),event.end(),compareevent);
int curheight=;
pair<int,int> tmp_pair;
for(int i=;i<event.size();i++)
{
if(event[i].side==)
{
activebuilding.insert(event[i].height_index);
int newheight=buildingheight[event[i].height_index];
int newx=event[i].x;
while(i+<event.size()&&event[i+].x==newx&&event[i+].side==)
{
i++;
activebuilding.insert(event[i].height_index);
newheight=max(newheight,buildingheight[event[i].height_index]);
}
if(newheight>curheight)
{
res.push_back(tmp_pair=make_pair(newx,newheight));
curheight=newheight;
}
}
else
{
activebuilding.erase(event[i].height_index);
int newheight=buildingheight[event[i].height_index];
int newx=event[i].x;
while(i+<event.size()&&event[i+].x==event[i].x&&event[i+].side==)
{
i++;
activebuilding.erase(event[i].height_index);
newheight=max(newheight,buildingheight[event[i].height_index]);
}
if(newheight==curheight)
{
int maxheight=;
multiset<int>:: iterator it=activebuilding.begin();
for(;it!=activebuilding.end();it++)
{
maxheight=max(maxheight,buildingheight[*it]);
}
if(maxheight<newheight)
{
res.push_back(tmp_pair=make_pair(newx,maxheight));
curheight=maxheight;
}
}
}
}
return res;
}
};

The Skyline Problem的更多相关文章

  1. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  2. [LeetCode] The Skyline Problem

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  3. [LeetCode#218] The Skyline Problem

    Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that ...

  4. [LeetCode] 281. The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  5. [LeetCode] 218. The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  6. Java for LeetCode 218 The Skyline Problem【HARD】

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  7. UVa 105 - The Skyline Problem(利用判断,在于想法)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  8. 218. The Skyline Problem *HARD* -- 矩形重叠

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  9. 218. The Skyline Problem

    题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...

随机推荐

  1. C++ RTTI

    一.定义:RTTI:Run Time Type Identification ,运行时类型识别:指程序能够使用基类的指针或引用来检索其所指对象的实际派生类型.二.使用方式:C++中有两个操作符提供RT ...

  2. android学习笔记 activity生命周期&任务栈&activity启动模式

    activity生命周期 完整生命周期 oncreate->onstart->onresume->onpause->onstop->ondestory 使用场景:应用程序 ...

  3. js动态加载css文件和js文件的方法

    今天研究了下js动态加载js文件和css文件的方法. 网上发现一个动态加载的方法.摘抄下来,方便自己以后使用 [code lang="html"] <html xmlns=& ...

  4. 大家一起和snailren学java-(七)多态

    “这个系列觉得没必要这么写,不然质量不会高,还是看一段时间,自己提炼吧” 多态,也称作动态绑定,后期绑定,是三个基本特征中非常重要的一个特征.通过多态,可以消除类型之间的耦合关系.同时多态提供了扩展程 ...

  5. Effective Java 68 Prefer executors and tasks to threads

    Principle The general mechanism for executing tasks is the executor service. If you think in terms o ...

  6. 搭建Struts2不同版本jar包不同

    struts2的版本比较多,所以在开发的时候特别要注意版本不同所需引入的包是不一样的.否则,会出现各种问题.而且很难找到问题所在. 以下是我遇到的问题总结: 一.当我运用struts2.3.4.1时, ...

  7. Eclipse報錯:Could not find or load main class

    代碼正確,但在Eclipse中無法運行,一直報錯: Could not find or load main class

  8. C#.net Winform获取文件路径

    C# 获取路径 string str1 =Process.GetCurrentProcess().MainModule.FileName;//获得当前执行的exe的文件名.string str2=En ...

  9. Super A^B mod C

    Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B ...

  10. python django 与数据库的交互

    下载没有任何问题的mysqdb http://www.codegood.com/archives/4 1创建一个新的app. python manage.py startapp books 2 激活a ...