单调栈: 维护一个单调栈

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. 限制TextBox输入,只能输入整数

    public class TextBoxInt : TextBox { public TextBoxInt() { KeyDown += TextBoxInt_KeyDown; TextChanged ...

  2. Mono For Android 实现索引分段器效果

    这两天看了个Demo,自己也动手写了下,觉得效果还不错,分享一下! 主要是利用ListActivity中自带了一个ListView和ListAdapter来实现的,当然也可以在一般的Activity中 ...

  3. 线性表A-B

    1.顺序存储 #include<stdio.h> /* 设有两个顺序表A和B,且都递增有序,试写一算法,从A中删除与B中相同的那些元素,即求A-B */ #define getArrayL ...

  4. linux 学习之九、Linux 磁盘与文件系统管理(3)

    原文地址:http://vbird.dic.ksu.edu.tw/linux_basic/0230filesystem.php#filesys 创建大文件以制作 loop 装置文件!(练习非常有用) ...

  5. 怎样在小方框上打对号 小方框内打对勾 word 方框打对勾

    在word中做选择时,非常多人遇到须要在小方框上打对勾而不知怎样做,现将可行的各种方法总结例如以下: 1:直接找到一个做好的,保存为图片,在须要的时候插入它: 2:插入文本框,然后边框选择为实线,在文 ...

  6. poj - 4045 - Power Station

    题意:一棵有n个结点的树,要取其中的一个结点,使得该结点到其他所有结点的距离和dis最小,即损耗I * I * R * dis最小,输出最小损耗和该结点(有多个的话按结点编号从小到大输出)(3 < ...

  7. [置顶] 浅析objc的消息机制

    学习ios的同学都知道ojbc一种runtime的语言,runtime表明函数的真正执行的时候来确定函数执行的.这样的好处就是我们能很灵活的设计我们的代码,也能在看似合法的情况下做一些非常有意思的事情 ...

  8. javascript高级知识点——实例类型

    代码信息来自于http://ejohn.org/apps/learn/. 分析一下对象的结构 function Ninja(){} var ninja = new Ninja(); console.l ...

  9. TCP应用编程--套接字C#实现

     套接字之间的连接过程可以分为三个步骤: 1.服务器监听 2.客户端请求 3.连接确认 Ø服务器监听:是指服务器套接字并不定位具体的客户端套接字,而 是处于等待连接的状态,实时监控网络状态. Ø客户端 ...

  10. static和extern关键字 对变量的作用

    本文目录 • 一.在Java中,全局变量的定义没有严格的位置规定 • 二.在C语言中,全局变量定义的位置是有限制的 • 三.重复定义同一个变量 • 四.不同源文件中的同名变量 • 五.static关键 ...