The Skyline Problem轮廓线问题算法详解
2017-06-02 15:18
627 查看
问题详见: 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).
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≤INTMAX, 0<Hi≤INTMAX, 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: [[2910],[3715],[51212],[152010],[19248]].
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:[[210],[315],[712],[120],[1510],[208],[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, [...[23],[45],[75],[115],[127]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[23],[45],[127],...]
解题思路:
由于题目中建筑物默认都是规则的矩形,求解的轮廓线的关键点是当前扫描且未扫描完成的建筑物的边缘角的坐标,所以考虑使用队列来储存当前扫描且未扫描完成的建筑物信息。当前扫描到的建筑物要么是全新的(即和之前扫描的建筑物没有任何交集),要么是和队列中的建筑物存在交集,此时只需根据新扫描的建筑物信息更新队列和轮廓线边缘角坐标即可。整个算法最差情况复杂度为O(nlog(n))。具体算法如下:class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) { vector<pair<int, int>> res; int cur=0, cur_X=0, cur_H=0, len=buildings.size(); priority_queue<pair<int, int>> liveBlg; while(cur<len || !liveBlg.empty()) { cur_X = liveBlg.empty()? buildings[cur][0]:liveBlg.top().second; if(cur>=len || buildings[cur][0] > cur_X) while(!liveBlg.empty() && (liveBlg.top().second <= cur_X) ) liveBlg.pop(); else{ cur_X = buildings[cur][0]; while(cur<len && buildings[cur][0]== cur_X) { liveBlg.push(make_pair(buildings[cur][2], buildings[cur][1])); cur++; } } cur_H = liveBlg.empty()?0:liveBlg.top().first; if(res.empty() || (res.back().second != cur_H) ) res.push_back(make_pair(cur_X, cur_H)); } return res; } };
其提交运行结果如下:
相关文章推荐
- Uva 101 the block problem 木块问题(算法竞赛经典入门)STL vector
- 算法分析课每周练习 The Skyline Problem
- 轮廓问题/Outline Problem-->改进的算法及时间复杂度分析
- [LeetCode] The Skyline Problem 天际线问题
- 算法课作业系列6——The Skyline Problem
- 【算法】The Skyline Problem
- 图灵停机问题(The Halting Problem)
- UVa Problem 100 The 3n+1 problem (3n+1 问题)
- 背包问题系列算法详解
- 105 - The Skyline Problem
- Avalon CTP : 黑窗问题 the black window problem
- Problem B. Investing at the Market 问题B 市场投资 分析 解决办法
- Problem B. Investing at the Market 问题B 市场投资 解决办法 Python方法
- 详解 Tomcat: The value for the useBean class attribute is invalid 问题
- 解决“A problem has been encountered while loading the setup components. Canceling setup.”的问题
- hdu_1048_The Hardest Problem Ever_字符输入输出问题水题一道
- 停机问题--The Halting Problem
- The problem of the user is not associated with a trusted sql server connection 混合登录选项设置的问题
- 表扩展失败(ORA-01653)后的空间管理问题【THE SPACE MANAGEMENT PROBLEM OF THE TABLE EXTEND FAILD 】 推荐
- 拜占庭将军问题 (The Byzantine Generals Problem)