单调栈: 维护一个单调栈

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. 利用C#轻松创建不规则窗体

    1.准备一个不规则的位图 可以使用任意一种你喜欢的作图工具,制作一个有形状的位图,背景使用一种其他的颜色.这个颜色在编程中用得着,所以最好使用一种容易记忆的颜色.如黄色,文件名为bk.bmp 2.创建 ...

  2. Python urllib和urllib2模块学习(二)

    一.urllib其它函数 前面介绍了 urllib 模块,以及它常用的 urlopen() 和 urlretrieve()函数的使用介绍.当然 urllib 还有一些其它很有用的辅助方法,比如对 ur ...

  3. SQL顺序列找出断号

    select id from info id-----------123567810111215 (11 行受影响) 方法一: select (select max(id)+1 from Info w ...

  4. Congos

    http://hi.baidu.com/tag/cognos%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86/feeds http://www.blogjava.net/mlh ...

  5. docker 数据管理3

    实际应用: 第一个容器使用: docker run -itd -v /data/:/data1 centos  bash // -v 用来指定挂载目录, 后面的容器使用之前的容器数据卷 docker: ...

  6. chrome浏览器debugger 调试,有意思。

    JavaScript代码中加入一句debugger;来手工造成一个断点效果. 例子: ajax看看返回的数据内容,或者想知道js变量获取值是什么的时候. $.ajax({ type:"pos ...

  7. java常量池理解

    String类两种不同的创建方式 String s1 = "zheng"; //第一种创建方式 String s2 = new String("junxiang" ...

  8. ubuntu 更改文件所有者

    参考资料:http://teliute.org/linux/Tecli/lesson13/lesson13.html sudo chown -R  username:groupname  filena ...

  9. linux内核函数库文件的寻找

    linux内核函数的so库文件怎么找呢? 首先还是要产生一个进程的coredump文件的 linux有一个lib-gdb.so库,这个进程的coredump文件中所有load段的最后一个load段中, ...

  10. react-native component function

    examples: use: