首页 > 其他分享 >单调栈--HDOJ4252A Famous City

单调栈--HDOJ4252A Famous City

时间:2022-10-26 20:41:53浏览次数:63  
标签:buildings -- ++ number Famous int HDOJ4252A Result he


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:

单调栈--HDOJ4252A Famous City_#include

#include<iostream>
#include<stack>
using namespace std;

stack<int> s;
int n,t;
int NumOfCase = 1;
int Result;

int main(){
while(scanf("%d",&n) != EOF){
Result = 0;
while(!s.empty()){
s.pop();
}
for(int i = 0;i < n;i ++){
scanf("%d",&t);
if(s.empty()){
if(t != 0){
s.push(t);
Result++;
}
}
else{
while(!s.empty() && (t < s.top())){
s.pop();
}
if(s.empty()){
if(t != 0){
s.push(t);
Result++;
}
}
else if(t > s.top()){
s.push(t);
Result++;
}
}
}
printf("case %d: %d\n",Result,NumOfCase++);
}
return 0;
}

 

标签:buildings,--,++,number,Famous,int,HDOJ4252A,Result,he
From: https://blog.51cto.com/u_13121994/5798365

相关文章

  • 单调栈--Largest Rectangle in a Histogram
    DescriptionAhistogramisapolygoncomposedofasequenceofrectanglesalignedatacommonbaseline.Therectangleshaveequalwidthsbutmayhavedifferent......
  • 单调栈--Feel Good
    DescriptionBillisdevelopinganewmathematicaltheoryforhumanemotions.Hisrecentinvestigationsarededicatedtostudyinghowgoodorbaddaysinfluentpe......
  • 并查集--翻译机的个数(顺丰2020年笔试)
    某学术会议上,一共有n个人参加,现在已知每个人会的语言(一个人可能不会任何语言)。现在有一种学习机,每一个学习机可以在会议期间使一个人暂时掌握一种自己不会的语言,问要使得任......
  • 最长非递减子序列--顺丰2020校招笔试题
    n的范围是[0,100000]DP版本(O(n^2))时间复杂度(LTE):#include<cstdio>#include<iostream>#include<algorithm>usingnamespacestd;#defineN100intmain(){intA[N],dp[N......
  • 字符串--移除k个数使得剩下的数最大
    有一十进制正整数,移除其中的K个数,使剩下的数字是所有可能中最大的。假设:字符串的长度一定大于等于K字符串不会以0开头 输入描述:一行由正整数组成的数字字符串,和......
  • DP--爬楼梯1
    题目描述前几个月放映的头号玩家简直火得不能再火了,作为一个探索终极AI的研究人员,月神自然去看了此神剧。由于太过兴奋,晚上月神做了一个奇怪的梦,月神梦见自己掉入了一个被施......
  • DP--字符串变换
    给定两个字符串,已知可以使用三种方式进行变换1.插入一个字符2.删除一个字符3.更改一个字符请设计一个算法,找到两个字符串之间的经历几次最小变换,可以字符串1转换成字......
  • DFS--同一个方向找出所有子字符串的个数
     字符迷阵是一种经典的智力游戏。玩家需要在给定的矩形的字符迷阵中寻找特定的单词。在这题的规则中,单词是如下规定的:1.在字符迷阵中选取一个字符作为单词的开头;2.选取......
  • map记录下标
    题目描述小云正在参与开发一个即时聊天工具,他负责其中的会话列表部分。会话列表为显示为一个从上到下的多行控件,其中每一行表示一个会话,每一个会话都可以以一个唯一正整数id......
  • DFS(全排列)--相同序号不能相邻
     小多想在美化一下自己的庄园。他的庄园毗邻一条小河,他希望在河边种一排树,共M棵。小多采购了N个品种的树,每个品种的数量是Ai(树的总数量恰好为M)。但是他希望任意......