单调栈: 维护一个单调栈

A Famous City

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1671    Accepted Submission(s): 644

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
Hint
The possible configurations of the samples are illustrated below:
 
Source
 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <stack> using namespace std; stack<int> stk;
int n; int main()
{
int cas=1;
while(scanf("%d",&n)!=EOF)
{
int ans=0;
while(!stk.empty()) stk.pop();
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
if(stk.empty())
{
if(x!=0) stk.push(x);
continue;
}
if(stk.top()==x) continue;
else if(stk.top()<x) stk.push(x);
else
{
bool flag=true;
while(!stk.empty())
{
if(stk.top()>x)
{
stk.pop(); ans++;
}
else if(stk.top()==x)
{
flag=false;
break;
}
else if(stk.top()<x)
{
if(x!=0) stk.push(x);
flag=false;
break;
}
}
if(flag)
{
if(x!=0) stk.push(x);
}
}
}
ans+=stk.size();
printf("Case %d: %d\n",cas++,ans);
}
return 0;
}

HDOJ 4252 A Famous City 单调栈的更多相关文章

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

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

  2. hdu 4252 A Famous City

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

  3. HDU-4252 A Famous City(单调栈)

    最后更新于2019.1.23 A Famous City ?戳这里可以前往原题 Problem Description After Mr. B arrived in Warsaw, he was sh ...

  4. 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...

  5. BZOJ 1628 [Usaco2007 Demo]City skyline:单调栈

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1628 题意: 题解: 单调栈. 单调性: 栈内元素高度递增. 一旦出现比栈顶小的元素,则表 ...

  6. 单调栈3_水到极致的题 HDOJ4252

    A Famous City 题目大意 给出正视图  每一列为楼的高度 最少有几座楼 坑点 楼高度可以为0 代表没有楼 贡献了两发RE 原因 if(!s.empty()&&tem){s. ...

  7. Gym 100971D Laying Cables 单调栈

    Description One-dimensional country has n cities, the i-th of which is located at the point xi and h ...

  8. BZOJ_1628_[Usaco2007_Demo]_City_skyline_(单调栈)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1628 给出\(n\)个距形的影子,问最少是多少个建筑的?(建筑的影子可以重叠). 分析 用单调 ...

  9. Code Forces Gym 100971D Laying Cables(单调栈)

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. 使用Idea编写javaweb以及maven

    使用Idea编写javaweb以及maven 今天总结的第一点是在windows下使用idea编写jsp并且使用tomcat部署:第二点是新建maven项目,之前一直是听说也没有自己实践过,今天就大概 ...

  2. MYSQ 查看 2 进制日志

    方法 1: myqlbinlog filename; ------------------------------------------------------------------------- ...

  3. Keil MDK中使用pc-lint的详细方法

    keil MDK版本:V4.03 PC-lint版本:  V8.0 关于pc-lint的强大作用,网上有很多,这里不想再复述,只说一句:能通过pc-lint检验的程序不一定没有问题,但通过了pc-li ...

  4. 前端js模板库 JinkoTemplate

    有时候需要使用ajax来异步生成html,最土的方法就是用js的‘+’连接html代码,生成繁琐.一旦需要修改,对于少量的html代码到没啥问题,要是比较复杂的样式时,就真坑爹了,眼花缭乱有木有?Ji ...

  5. Gradient boosting

    Gradient boosting gradient boosting 是一种boosting(组合弱学习器得到强学习器)算法中的一种,可以把学习算法(logistic regression,deci ...

  6. shell programs

    find * -not -path "docs/*" -regex ".*\.\(rb\)" -type f -print0 | xargs -0     gr ...

  7. Aix 文件名补齐及aix6.1 bash安装

    Aix历史查询快捷键=>按ESC+k 设置KSH的自动补全(仅仅是文件名补全,没有命令补全)和历史命令功能 方法一: set -o vi 历史命令功能(esc -,esc +)自动补全文件名(e ...

  8. andengine游戏引擎总结进阶篇1

    本篇包括虚拟键盘,粒子系统 1虚拟键盘 分为两种,一种是单个虚拟键盘,另一种是多个方位虚拟键盘 1)加载虚拟键盘所需要的图片资源 private BitmapTextureAtlas mOnScree ...

  9. 【Leetcode】Same Tree

    给定两棵二叉树,判断是否相等(即树的结构以及各结点中的值都一样) Given two binary trees, write a function to check if they are equal ...

  10. ORACLE 如何查询被锁定表及如何解锁释放session

    ORACLE EBS操作某一个FORM界面,或者后台数据库操作某一个表时发现一直出于"假死"状态,可能是该表被某一用户锁定,导致其他用户无法继续操作 --锁表查询SQLSELECT ...