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. yii url美化 urlManager组件

    yii的官方文档对此的解释如下: urlSuffix  此规则使用的url后缀,默认使用CurlManger::urlSuffix,值为null.例如可以将此设置为.html,让url看起来“像”是一 ...

  2. Android v4、v7、v13 的区别

    Android Support v4:  这个包是为了照顾1.6及更高版本而设计的,这个包是使用最广泛的,eclipse新建工程时,都默认带有了. Android Support v7:  这个包是为 ...

  3. C++ RTTI

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

  4. UIlable 属性详用

    我的好朋友给我制定了一个新的学习方法,从新的看每个控件,去了解他的每个属性,方法来让自己对oc的认识更加充实 今天重新认识一下UILable 的属性lable的阴影设置: 阴影的偏移量是以lable中 ...

  5. .NET下的并行开发

    并行开发一直是程序员在开发项目中遇到的一道坎,但为了迎合硬件的升级,面对高端多核的处理器,并行编程势在必行.在.NET平台下的开发支持并行模式,下面用一个实际项目说明并行的高效率和神奇之处. 在优化中 ...

  6. Java 之 内部类

    (static修饰的成员属于整个类,而不属于单个对象) 定义:将一个类放到另一个类的内部定义,这个在内部定义的类就叫做内部类(也有的成嵌套类),包含内部类的类被称为外部类(也有的称宿主类). 1.非静 ...

  7. CentOS6.5安装mysql5.1.73

    思路: 1.查看有无安装过mysql rpm -qa|grep mysql

  8. Effective Java 77 For instance control, prefer enum types to readResolve

    The readResolve feature allows you to substitute another instance for the one created by readObject ...

  9. springMVC 接收数组参数,mybatis 接收数组参数,mybatis批量插入/批量删除案例

    案例是给一个用户赋予多个权限,多个权限用其对应的主键 id 为参数,组成了 一个id数组,传给springMVC,然后springMVC传给mybatis,然后mybatis批量插入.其实类似的场景还 ...

  10. R语言绘图高质量输出

    R语言通过支持Cairo矢量图形处理的类库,可以创建高质量的矢量图形(PDF,PostScript,SVG) 和 位图(PNG,JPEG, TIFF),同时支持在后台程序中高质量渲染.在ggplot2 ...