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

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

解题思路:

当看到题目时,仔细想想感觉用暴力方法挺复杂的.需要判断建筑是否重叠,找到高度的变化位置,找出对应的左值,找出对应的右值.能这么想就很接近我要讲的解题方法了.

我们是需要找到高度转折点,找到最大的右值.

高度转折点的寻找:判断高度是否和前面的高度不同.但是这个建筑物是二维图形,如何去找建筑的分界点有些很复杂.由于题目中所给的限定,我们可以把建筑分成不连续的正整数点所围成.这样利用for循环,每次横坐标的值加1.直到高度变化之后,说明这里需要输出.(前提是我们要把这些不连续的正整数横坐标确定之后找出他们的最大的高度,利用for循环,横坐标每次加1,不断修改某一点的高度值)

下面给出代码:

 1 #include<bits/stdc++.h>
 2 #define MAX 10010
 3
 4 using namespace std;
 5
 6 int height[MAX];
 7
 8 int main()
 9 {
10     int i,l,h,r,max_right=-1;
11     while (~scanf("%d%d%d", &l, &h, &r))
12     {
13         memset(height,0,sizeof(height));
14         for (i=l; i<r; i++)
15             height[i]=max(height[i], h); //注意右端点处不记录高度
16         max_right=max(max_right,r);
17     }
18     for (i=1; i<max_right; i++)
19         if (height[i]!=height[i-1])
20             printf("%d %d ",i,height[i]);
21     printf("%d 0\n", max_right);
22     return 0;
23 }

推荐博客链接:

http://www.cnblogs.com/devymex/archive/2010/08/07/1794533.html

时间: 2024-10-11 03:43:42

105 - The Skyline Problem(利用判断,在于想法)的相关文章

uva 105 - The Skyline Problem

一.用数组储存该位置的最高点即可(图形的连续点离散化),注意左边界及右边界的情况: 注意:无论建筑物最左边是盖到哪里,你都得从1开始输出(输入输出都是integer,所以才能离散化): 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 int main() 6 { 7 int skyline[10005] = {0}; 8 int L, H, R; 9 int rightest = 0; 10

[LeetCode-JAVA] The Skyline Problem

题目:题目太长了,见链接-- > The Skyline Problem 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

HDU 1086 You can Solve a Geometry Problem too(判断线段相交)

题目地址:HDU 1086 就这么一道仅仅判断线段相交的题目写了2k多B的代码..是不是有点浪费...但是我觉得似乎哪里也优化不了了.... 判断线段相交就是利用的叉积.假如现在两条线段分别是L1和L2,先求L1和L2两个端点与L1的某个端点的向量的叉积,如果这两个的叉积的乘积小于0的话,说明L1在是在L2两个端点之间的,但此时并不保证一定相交.此时需要用同样的方法去判断L2是否在L1的两个端点之间,如果L2也在L1的两个端点之间的话,那就足以说明L1与L2相交.但是这题还需要判断是否端点也相交

HDU 5170 GTY&#39;s math problem 精度判断问题

传送门 GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 568    Accepted Submission(s): 263 Problem Description GTY is a GodBull who will get an Au in NOI . To have more time to l

实验12:Problem D: 判断两个圆之间的关系

Home Web Board ProblemSet Standing Status Statistics Problem D: 判断两个圆之间的关系 Problem D: 判断两个圆之间的关系 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 381  Solved: 325[Submit][Status][Web Board] Description 定义Point类,包括double类型的两个属性,分别表示二维空间中一个点的横纵坐标:定义其必要的构

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 pr

[LeetCode] The Skyline Problem

An interesting problem! But not very easy at first glance. You need to think very clear about how will a keypoint be generated. Since I only learn from others' solutions and am still unable to give my personal explanations, I would suggest you to the

[LeetCode] 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 pr

解题报告:LeetCode The Skyline Problem(画天际

题目出处:https://leetcode.com/submissions/detail/47013144/题意描述: 给定一系列矩形的左边坐标Li,右边坐标Ri,和高度Hi(其中Li按照从小到大的顺序排列).代表城市中一座座高楼.求这些矩形代表的高楼行成的天际线.天际线的定义为:在远处看这些所有的高楼时看到的轮廓. 数据输入: [ [L1, R1, H1], [Li, Ri, Hi]...]为元素类型为三维向量的向量,其中Li,Ri,Hi的含义见题意描述.且输入数据保证: 0 ≤ Li, Ri