最后更新于2019.1.23

A Famous City

?戳这里可以前往原题

Problem 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

题目大意:

将一张照片从左到右分为N个垂直部分。照片中的建筑物可以被视为矩形,一栋建筑可以跨越几个连续的部分,但每个部分只能包含一个可见的建筑,或者根本没有建筑。根据每个部分的高度,求出照片中最少建筑物数量

解题思路:

考虑最优情况,及每一种高度就只有一个建筑物,则最少建筑物数量及等于高度的种数。

根据高的建筑物覆盖低的建筑物原则,也就是说,如果两个矩形之间的所有建筑物的高度都大于等于这两个矩形,且这两个矩形高度相等,我们就可以把这两个矩形视为同一个建筑物。反过来,如果这两个矩形之间的建筑物中出现了更低的矩形,那么这两个就一定不能连成一个建筑物。

推广上面的结论:

1、如果所有的矩形高度维持从小到大的顺序,则最少建筑物数量及等于高度的种数。

2、如果出现了比上一个矩形更低的矩形,则上一个矩形所对应的建筑物不能再与后面的建筑物连成一个,则我们可以结算掉前面已经出现的矩形中,高于新加入的矩形的个数。

也就是说,我们需要把不断递增的序列保存下来,然后维护这个序列保持严格递增,即可得到答案,即维护一个单调栈。

下面是AC代码

#include <bits/stdc++.h>

using namespace std;

stack<int> st;//栈

int main()
{
ios::sync_with_stdio(false); int n;
int cases=1;
int answer;
while (cin>>n)
{
answer=0;
int temp;
//由于我们最后计算个数时必然会把栈内的数据全部弹出,则不需要清空栈
while (n--)
{
cin>>temp;
if (st.empty())//如果栈内为空,则直接推入栈
{
st.push(temp);
continue;
}
//如果出现比栈内所有值都高的一个值,则可以直接入栈(由于我们维护的是单调栈,所以栈顶的元素一定是最大的,所以只需要比较栈顶的元素)
if (temp>st.top())
{
st.push(temp);
}
else if (temp<st.top())//反之则把大于新的数据的值都从栈中弹出
{
while ((!st.empty()) && (st.top()>temp))
{
answer++;
st.pop();
}
if (st.empty())//如果已经弹空
{
st.push(temp);
continue;
}
if (st.top()!=temp)//如果将要入栈的值与上一个值发生重复,则不需要压入栈,这里判断的是不重复的情况
{
st.push(temp);
}
}
}
while(!st.empty())//结算栈中剩下的部分
{
if(st.top())//这里是此题的一个坑,因为可能出现高度为0的情况,而0是没有建筑的
{
answer++;
}
st.pop();
}
cout<<"Case "<<cases<<": "<<answer<<endl;
cases++;
}
return 0;
}

HDU-4252 A Famous City(单调栈)的更多相关文章

  1. HDOJ 4252 A Famous City 单调栈

    单调栈: 维护一个单调栈 A Famous City Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. hdu 4252 A Famous City

    题意:一张相片上的很多建筑相互遮住了,根据高低不同就在相片上把一座高楼的可见部分作为一个矩形,并用数字描述其高度,若一张相片上的两个建筑群中间有空地,高度则为0;求最少有多少个建筑; 分析: 输入的0 ...

  3. bnuoj 25659 A Famous City (单调栈)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659 #include <iostream> #include <stdio.h ...

  4. HDU 5875 H - Function 用单调栈水过了

    http://acm.hdu.edu.cn/showproblem.php?pid=5875 单调栈,预处理to[i]表示第一个比a[i]小的数字,一直跳就可以. 这题是数据水而已. 这里学习下单调栈 ...

  5. HDU 4923 Room and Moor (单调栈)

    题意: 给你一个A数列,让你求一个单调递增的B数列(0<=bi<=1),使得sum{(ai-bi)^2}最小. 思路: 很明显,如果A = 0...01...1,那么bi=ai即可. 可以 ...

  6. hdu 5696 区间的价值 单调栈+rmq

    区间的价值 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  7. hdu 4923 Room and Moor (单调栈+思维)

    题意: 给一个0和1组成的序列a,要构造一个相同长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得  sum((ai-bi)^2)最小. 算法: 首先a序列開始的连续0和末尾的连续1是能 ...

  8. HDU - 5033: Building(单调栈 ,求一排高楼中人看楼的最大仰角)

    pro:现在在X轴上有N个摩天大楼,以及Q个人,人和大楼的坐标各不相同,保证每个人左边和右边都有楼,问每个人能看到天空的角度大小. sol:不难想到就是维护凸包,此题就是让你模拟斜率优化,此处没有斜率 ...

  9. HDU 5033 Building(单调栈)

    HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...

随机推荐

  1. 日志框架之2 slf4j+logback实现日志架构 · 远观钱途

    如何从缤纷复杂的日志系统世界筛选出适合自己的日志框架以及slf4j+logback的组合美妙之处?此文可能有帮助 logback介绍 Logback是由log4j创始人设计的另一个开源日志组件,官方网 ...

  2. Atom配置(VIM党) · iuunhao

    为什么说是Vim党呢?首先我是一个深度的Vim用户,自己的电脑上基本上可以兼容Vim的插件都有,所有浏览器,所有编辑器都是Vim的操作方式,当然包括我现在书写的markdown的软件EME也是兼容的V ...

  3. 算法拾遗[4]——STL用法

    主要bb一下优先队列和字符串吧. 哦还有 bitset. 优先队列 定义很容易: priority_queue<int> pq; 内部是一个堆. 基本操作 pq.top() 取堆顶元素; ...

  4. 使用hexo,创建博客

    下载hexo工具 1 npm install hexo-cli -g 下载完成后可以在命令行下生成一个全局命令hexo搭建博客可用thinkjs 创建一个博客文件夹 1 hexo init 博客文件夹 ...

  5. C++走向远洋——55(项目一3、分数类的重载、>>

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  6. sublime 安装Anaconda插件 配置python开发环境

    我的sublime 3  python 3.6.6 安装Anaconda插件 由于Anaconda插件本身无法知道Python安装的路径,所以需要设置Python主程序的实际位置.选择Settings ...

  7. java 初探-猜数字

    学习Java而编写的猜数字的程序代码. /** * */ package org.literal.test; import java.util.Scanner; /** * @author liter ...

  8. file_put_contents生成ansi文件

    $line_body = array('张三','李四','王五'); $line_body = array_map(function ($element){return iconv('UTF-8', ...

  9. javascript输入用户名,在服务器中显示出了

    ## 第一个文件 pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Tra ...

  10. 『配置』服务器搭建 Office Online Server2016 实现文档预览 番外 错误篇

    安装一个或多个角色.角色服务或功能失败.找不到源文件.请再次尝试在新的“添加角色和功能”向导会话中安装角色.角色服务或功能,然后在向导的“确认”页中单击“指定备用源路径”以指定安装所需的源文件的有效位 ...