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. LoadBalancer在kubernetes架构下的实践

    Backgound 借助于kubernetes优秀的弹性扩缩功能,运行其中的应用程序能够在流量突增的时候坦然应对,在流量低谷的时候无需担心成本.但于此同时,也带来了极大的挑战: 弹性扩缩导致容器IP动 ...

  2. 二刷Redux笔记

    关于react的一些思考 所有的数据全部先要发送给容器,然后容器负责接受数据单后再分发数据给他下面的组件,通过props来传递,一个页面就可以相当于一个容器,容器之中就会有很多子组件,一般组件只负责接 ...

  3. 化学元素周期表的英文全称 Periodic Table of the Elements

    化学元素周期表的英文全称 Periodic Table of the Elements   缩写 PTE 拉丁文 英文 1 H 氢 Hydrogenium Hydrogen 2 He 氦 Helium ...

  4. Android_存储之SharedPreferences

    一.概述 SharedPreferences是一种轻量级的数据存储方式,采用键值对的存储方式. SharedPreferences只能存储少量数据,大量数据不能使用该方式存储,支持存储的数据类型有bo ...

  5. 第 7 篇:文章详情的 API 接口

    作者:HelloGitHub-追梦人物 一旦我们使用了视图集,并实现了 HTTP 请求对应的 action 方法(对应规则的说明见 使用视图集简化代码),将其在路由器中注册后,django-restf ...

  6. 货车运输 noip2013 luogu P1967 (最大生成树+倍增LCA)

    luogu题目传送门! 首先,题目让我们求每个货车的最大运输量,翻译一下就是求路径上边权最小的边. 利用一下贪心思想可知,所有货车肯定都会尽量往大的边走. 进一步翻译,即为有一些小边货车根本不会走,或 ...

  7. 设计Weekday类 代码参考

    #include <iostream> using namespace std; class Weekday { private: int num; public: void SetDay ...

  8. Spring/SpringBoot常用注解总结

    转自:[Guide哥] 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使 ...

  9. win服务器管理软件巧利用——如何让服务器管理事半功倍

    那些服务器管理大牛估计看到这个标题会笑了,服务器怎么管理,靠自带的远程桌面肯定是远远不够的,要实现上千台服务器同时登陆,没有一个好程序管理,估计得三餐不食为其颠倒. 那么,有什么好的服务器推荐呢?站长 ...

  10. Spring boot Sample 007之spring-boot-log4j2

    一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合多环境log4j2 三.步骤 3.1.点击File -> New Project -> ...