博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ 4252 A Famous City 单调栈
阅读量:4709 次
发布时间:2019-06-10

本文共 1994 字,大约阅读时间需要 6 分钟。

单调栈: 维护一个单调栈

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 
#include
#include
#include
#include
using namespace std;stack
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
x) { stk.pop(); ans++; } else if(stk.top()==x) { flag=false; break; } else if(stk.top()

转载于:https://www.cnblogs.com/zfyouxi/p/5204748.html

你可能感兴趣的文章
C# 多线程学习系列二
查看>>
如何将你的github仓库部署到github pages(转)
查看>>
几个重要的shell命令:diff patch tar find grep
查看>>
学习笔记
查看>>
JS ES6 -- let命令
查看>>
查找空座位问题
查看>>
几个简单规则改进你的SEO效果
查看>>
UVA10820 Send a Table
查看>>
主流css reset的讲解分析(转载)
查看>>
OpenCV4Android Tutorial0解析
查看>>
Oracle数据库(一)
查看>>
SVD与文本摘要
查看>>
HDU 5451 广义斐波那契数列
查看>>
mysql5.6配置文件my.ini位置
查看>>
[BZOJ4820][SDOI2017]硬币游戏(高斯消元+KMP)
查看>>
构造矩阵解决这个问题 【nyoj299 Matrix Power Series】
查看>>
记点笔记
查看>>
网络编程——第三篇 HTTP应用编程(下)
查看>>
进程管理(Process类)
查看>>
Android进阶篇-访问Https链接
查看>>