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. System.Web.mail ----虚拟发件人发送邮件

     转载别人的 使用SMTP发送邮件   说到邮件发送,先提一下SMTP. SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议.它是一组用于从源地址到目的 ...

  2. 基于elementUI使用v-model实现经纬度输入的vue组件

    绑定一个 [12.34,-45.67] (东经西经,南纬北纬 正负表示) 形式的经纬度数组,能够按度分秒进行编辑,效果如下所示,点击东经,北纬可切换. 经纬度的 度转度分秒 能够获取度分秒格式数据 C ...

  3. codeforce E. Fire背包

    E. Fire time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  4. JPA EntityManager 在没有实体类的情况下返回Map

    JPA entityManager.createNativeQuery()执行原生的SQL,当我们查询结果没有对应的实体类时,query.getResultList()返回的是一个List<Ob ...

  5. #442-Find All Duplicates in an Array-数组中重复的数字

    一.题目 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...

  6. CF1340B Nastya and Scoreboard(暴搜剪枝/dp)

    Question 一个n个数码位的分数板,每一个数码位都是一个七段数码管,现在给出每个数码位的显示情况,问再点亮k段数码管的话能显示的最大的数是多少,如果不能构成一串数字,就输出-1 Solution ...

  7. 又抓了一个导致频繁GC的鬼--数组动态扩容

    概述 本周有个同事过来咨询一个比较诡异的gc问题,大概现象是,系统一直在做cms gc,但是老生代一直不降下去,但是执行一次jmap -histo:live之后,也就是主动触发一次full gc之后, ...

  8. 【JAVA习题七】输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

    package erase; import java.util.Scanner; public class 字符串分类 { public static void main(String[] args) ...

  9. [COCOS2DX-LUA]0-003.根据COCOS2DX热更新

    一.最近有需求就是要基于COCOS2DX-LUA进行游戏的增量更新,找了资料,发现了COCOS2DX有自带一个热更新的类,就是AssetsManager,但是该接口对于我来说有以下的缺陷 1.版本号在 ...

  10. [JavaWeb基础] 018.Struts2 Action通配符使用

    Struts2中有一个很牛逼的action通配符,可以用来简化action配置,以我们将要讲解的案例来说,如果我们要对一个学生信息进行增加,删除,修改,那么按照原来的做法,我们需要写3个Action来 ...