首页 技术 正文
技术 2022年11月10日
0 收藏 497 点赞 2,663 浏览 2161 个字

Largest Rectangle in a Histogram

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19782   Accepted: 6393

Description

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles: 
poj 2559  Largest Rectangle in a Histogram – 单调栈
Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.

Input

The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1<=n<=100000. Then follow n integers h1,…,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.

Output

For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

Sample Input

7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

Sample Output

8
4000

Hint

Huge input, scanf is recommended. 

/*/
大二这学期开学真是忙爆了。作为一个班干部,天哪。。好久没有刷题了,想起以前听了岛娘的一节课,单调栈,好像有点似懂非懂,于是抽时间看了一下,总算是搞通了。。。将读入的数据一个个压栈,奖数据与栈顶进行比较大小,如果这个数比栈顶小,就计算以站顶为高的最大矩形的大小,弹出栈顶。如果这个数比栈顶大,直接就压进栈。这样一系列操作之后,就会发现栈里面剩下一个递增数列,用一个pair来保存此时栈高度和前面达到这个高度的个数,然后按照前面的思想去计算每一个高度最大矩形面积是多大。

poj 2559  Largest Rectangle in a Histogram – 单调栈

AC代码:
/*/
#include "stdio.h"
#include "string.h"
#include "stack"
#include "algorithm"
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MX = 1e5 + 5;
int main() {
LL n,h;
while(~scanf("%lld",&n)) {
          if(n==0)break;
stack<PII> Q;
while(!Q.empty()) {
Q.pop();
}
LL ans=0;
for(LL i=0; i<n; i++) {
scanf("%lld",&h);
LL Now_Big_W=0;
while(!Q.empty() && Q.top().first >= h ) {
LL H = Q.top().first;
LL W = Q.top().second;
Q.pop();
Now_Big_W+=W;
ans=max(ans,H*Now_Big_W);
}
Q.push(PII(h,Now_Big_W+1));
}
LL The_number_W=0;
while(!Q.empty()){
The_number_W+=Q.top().second;
ans=max(ans,Q.top().first*The_number_W);
Q.pop();
}
printf("%lld\n",ans);
}
return 0;
}

  

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,285