Largest Rectangle in a Histogram

题目链接(点击)来源poj 2559

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: Description

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.

题意:

给出柱状图各个柱子的高度,计算最大矩形面积。

思路:

对每个柱子找到最大的宽的长度,将面积存进数组sort,输出最大面积。

具体实现:

要利用单调栈,下面给出两种代码,分别是记录左右区间和不记录区间只找出最大宽度。

1、记录左右区间(例题:Largest Rectangle in a Histogram) 感谢:Frank__Chen

#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL MAX=1e5;
LL n;
LL high[MAX+5];
LL top;
LL Stack[MAX+5],l[MAX+5],r[MAX+5];
int main()
{
while(scanf("%lld",&n)!=EOF){
if(n==0){
break;
}
for(LL i=1;i<=n;i++){
scanf("%lld",&high[i]);
}
high[0]=-1,high[n+1]=-1;
top=0;
Stack[top]=0;
for(LL i=1;i<=n;i++){
while(high[Stack[top]]>=high[i]) top--;
l[i]=Stack[top];
Stack[++top]=i;
}
top=0;
Stack[top]=n+1;
for(LL i=n;i>=1;i--){
while(high[Stack[top]]>=high[i]) top--;
r[i]=Stack[top];
Stack[++top]=i;
}
LL maxx=-MAX;
for(LL i=1;i<=n;i++){
maxx=max(maxx,(r[i]-l[i]-1)*high[i]);
}
printf("%lld\n",maxx);
}
return 0;
}

2、直接利用单调栈性质计算最大宽度   感谢:Parsnip_

Largest Rectangle in a Histogram【单调栈模板】的更多相关文章

  1. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  2. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  3. POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831 ...

  4. hdu 1506 Largest Rectangle in a Histogram(单调栈)

                                                                                                       L ...

  5. po'j2559 Largest Rectangle in a Histogram 单调栈(递增)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29498 ...

  6. POJ2559 Largest Rectangle in a Histogram —— 单调栈

    题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Lim ...

  7. HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

    题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...

  8. POJ2559 Largest Rectangle in a Histogram 单调栈

    题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...

  9. PKU 2559 Largest Rectangle in a Histogram(单调栈)

    题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...

  10. Largest Rectangle in a Histogram /// 单调栈 oj23906

    题目大意: 输入n,,1 ≤ n ≤ 100000,接下来n个数为每列的高度h ,0 ≤ hi ≤ 1000000000 求得最大矩阵的面积 Sample Input 7 2 1 4 5 1 3 34 ...

随机推荐

  1. 设置TextField的响应View和toolBar

    inputView  设置用于展示的响应View 类似于键盘的展示方式 inputAccessoryView 用于设置响应View上面的ToolBar 使用方式: inputView设置为响应View ...

  2. HDU3829 Cat VS Dog

    题目链接:https://vjudge.net/problem/HDU-3829 题目大意: 有\(P\)个小孩,\(N\)只猫,\(M\)只狗.每个小孩都有自己喜欢的某一只宠物和讨厌的某一只宠物(其 ...

  3. pyenv,轻松切换各种python版本

    pyenv,轻松切换各种python版本 解决什么问题 mac自带python2,md又不能删掉他 linux也自带python2,这玩意都过时了,也不赶紧换掉 安装pyenv git 安装 git ...

  4. SQL——SQL日期

    SQL日期    MySQL:        NOW() 返回当前的日期和时间        CURDATE() 返回当前的日期        CURTIME() 返回当前的时间        DAT ...

  5. Maven快速入门(一)Maven介绍及环境搭建

    做开发的程序员都知道,在系统开发需要各自各样的框架.工具.其中有一种工具不管你是初级程序员还是高级程序员都必须熟练掌握的,那就是项目管理工具(maven.ant.gradle).接下来就总结Maven ...

  6. 函数:exit()

    函数名: exit() 所在头文件:stdlib.h(如果是"VC6.0"的话头文件为:windows.h) 功 能: 关闭所有文件,终止正在执行的进程. exit(1)表示异常退 ...

  7. [Unity2d系列教程] 003.Unity如何调用android的方法

    Unity开发的时候很多时候我们需要用到底层的一些功能,比如摄像,录音,震动等等,我们在Unity的层面是无法完成的.那么我们考虑到Unity是否可以直接调用到android方面的方法,替我们去完成我 ...

  8. [SD心灵鸡汤]003.每月一则 - 2015.07

    乔布斯去世了,但他留给世人的财富却很多,值得每个人学习.他是个精力充沛魅力无限的家伙,同时也是一个很会鼓动人心的激励大师,甚至在他的平常对话中,经典的语句也常常脱口而出. 这里摘取了一些他的经典语录, ...

  9. 应小姐姐要求,整理常用Git操作命令,她都学会了,你确定不收藏

    前言 因为个人原因,转化了部门之后已经很久没有接触过开发层级的东西了,好多东西基本都忘记了,但是新的部门有时候会用到相应的研发部的代码和文档手册,所以耳边就充斥这一句话 这个为什么下载不了?这个为什么 ...

  10. python基本操作-文件、目录及路径

    目录 1 前言 2 文件夹操作 2.1 查询操作 2.2 创建操作 2.3 删除操作 2.4 修改操作 3 文件操作 3.1 查询操作 3.2 创建操作 3.3 修改操作 3.4 删除 4 路径操作 ...