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

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

A Famous City

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

Total Submission(s): 2855    Accepted Submission(s): 1073

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
 
题目大意是将下面的不同高度的区域,分割成尽量少的矩形,问矩形的数目
 
对于每一个区域,如果它向左在到达高度小于它的区域之前
出现了高度等于它的区域那么说明它能和该区域划分到同一矩形里面。
否则划分为新的矩形区域。
比如1 2 1,第三个1,向左找到第一个1,划分在同一矩形中。
根据这个属性,也就是说,每个区域左边大于它的部分是不相关的,
所以可以用单调栈进行处理。
如要注意的是,如果某个区域的高度为0,则只起分割作用。
 
代码如下:
#include
#include
#include
#include
using namespace std;const int MAXN=1e5+10;int n;int a[MAXN];int main(){ int Case=0,ans; ios::sync_with_stdio(false); while(cin>>n) { Case++; ans=0; for(int i=1;i<=n;i++) cin>>a[i]; stack
S; for(int i=1;i<=n;i++) { if(a[i]==0) { while(!S.empty()) S.pop(); } else { if(S.empty()) { S.push(i); ans++; } else { while(!S.empty()&&a[S.top()]>a[i]) S.pop(); if(S.empty()||a[S.top()]

 

 

转载于:https://www.cnblogs.com/a249189046/p/8724665.html

你可能感兴趣的文章
Ubuntu Server安装图形界面全过程
查看>>
Laravel 返回 JSON 格式
查看>>
magento 修改 paypal order product name
查看>>
JBoss 系列十八:使用JGroups构建块RpcDispatcher构建群组通信应用
查看>>
jsp struts标签迭代各种数据
查看>>
GIS中要素的捕捉以及C++实现
查看>>
存储过程
查看>>
微信开发之 微信支付
查看>>
HTML
查看>>
jsp
查看>>
133. Clone Graph
查看>>
Web服务器优化
查看>>
JSP之request表单的两种提交及乱码处理
查看>>
修改UITableView titleForHeader 的风格样式
查看>>
iOS 之sdwebimage
查看>>
Spring Boot 2 快速教程:WebFlux 快速入门(二)
查看>>
ASP.NET XML与JSON
查看>>
java程序员面试----交流项目经验(摘自百度)
查看>>
01-语言入门-01-A+B Problem
查看>>
冒泡排序--c#
查看>>