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. [linux]CentOS无法使用epel源

    [linux]CentOS无法使用epel源 问题的产生与解决 作者作为Android应用开发者,对linux的接触一直是ubuntu为主,但是有一个用于科学上网的vps,由于内存只有64M,所以使用 ...

  2. Effective Java 阅读笔记——方法

    38:检查参数的有效性 每当编写方法或者构造器的时候,应该考虑它的参数有哪些限制,在方法的开头处对参数进行检查,并且把这些限制写入文档. 注意: 对于公有方法,应该使用@throws标签在文档中说明违 ...

  3. 协议(Protocol)---实例

    协议:声明一些必须实现的方法和选择实现的方法,用来声明一些方法,即一个Protocol是由一系列的方法声明组成的. 建立协议文件步骤:将鼠标放到文件列表处,利用快捷键 command +N 健,得到如 ...

  4. Effective Java 51 Beware the performance of string concatenation

    Using the string concatenation operator repeatedly to concatenate n strings requires time quadratic ...

  5. 连接Oracle远程数据库错误:ORA-12541,ORA-12514,ORA-01017的解决方法!

    1.出现如下错误:ORA-12541:TNS:no listener,如下图所示: 错误原因是我们没有开启Listener监听器服务,解决方法是在服务中开启这个服务,如下图所示. 2.出现如下错误:O ...

  6. BaseDao

    public class BaseDao { private static Log logger = LogFactory.getLog(BaseDao.class); // 查询数据 public ...

  7. 深度学习笔记------linux下配置安装caffe-cpu only模式

    本文适合初学者学习配置caffe的最简单方法.这是我在看了网上各种大神的方法后总结的最傻瓜式的配置方法了. 本人也是在不断地配置caffe过程中受打击与成长@.@ 第一步.配置caffe所需的依赖环境 ...

  8. Less里css表达式的写法

    项目中用的grunt-contrib-less, 写了以下less代码 .mapfix{ position: fixed; top:10px; width: 430px; z-index: 100; ...

  9. sql server 之函数小技巧 && 整数类型为空是用空字符串替代实现

    1.判空函数 说明:使用指定的替换值替换 NULL. 语法:ISNULL ( check_expression , replacement_value ) 参数: check_expression:将 ...

  10. 数据库配置文件 conf.properties

    #数据库配置 #mysql hibernate.dialect =org.hibernate.dialect.MySQLDialect jdbc.driverClassName =com.mysql. ...