标签:
Description After Mr. B arrived in Warsaw, he was shocked by the skyscrapers and took several photos. But now when he looks at these photos, he finds in surprise that he isn‘t able to point out even the number of buildings in it. So he decides to work it out as follows: - divide the photo into n vertical pieces from left to right. The buildings in the photo can be treated as rectangles, the lower edge of which is the horizon. One building may span several consecutive pieces, but each piece can only contain one visible building, or no buildings at all. - measure the height of each building in that piece. - write a program to calculate the minimum number of buildings. Mr. B has finished the first two steps, the last comes to you. Input Each test case starts with a line containing an integer n (1 <= n <= 100,000). Following this is a line containing n integers - the height of building in each piece respectively. Note that zero height means there are no buildings in this piece at all. All the input numbers will be nonnegative and less than 1,000,000,000. Output For each test case, display a single line containing the case number and the minimum possible number of buildings in the photo. Sample Input 3 1 2 3 3 1 2 1 Sample Output Case 1: 3 Case 2: 2 图片: http://7xjob4.com1.z0.glb.clouddn.com/6e29e0ea1c39c19ca5749fd895f92322 先解释一下题目大意,题目假定所有建筑物是标准矩形,每一个建筑物楼顶是水平的。不同建筑物高度不同或相同,然后你向一群建筑物看去(不是一排,会有前后遮挡),然后你看到了建筑物群的大体轮廓,让你猜最少存在几栋建筑? 其实就是所有数据的个数和然后去重,去掉的是可能与之前出现的数据为同一个建筑的数据,但是这不是set容器可以轻松解决的,因为两个等高数据之间如果出现更低的数据,就可以肯定这两个数据不是同一个建筑。否则就视为同一个建筑。 怎么解决这个问题,假设我从左向右计数,如果右边出现数据比左边高什么都说明不了(放入栈中),假如右边出现一个小数据,那么就决定了左边的高楼不可能是和接下来的是高楼连在一起的,然后出栈并计数出栈的高楼数,若高楼出栈后准备进栈的高度发现栈顶是一个和自己一样的高度,那么这两部分就视为被中间高楼轮廓遮挡的同一个建筑,准备进栈数据就是一个重复数据,直接丢掉。。。。。。直到所有数据出栈。 有一个坑点就是高度为0的不算建筑。。。。。 那么见一个简洁的AC代码: #include标签:
原文地址:http://www.cnblogs.com/conquer-sea/p/5661297.html