Largest Rectangle in a Histogram
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1012 Accepted Submission(s): 335
Problem 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:
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.
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
#include<stdio.h> #define M 100005 int n,i,l[M],r[M]; long long max,term,p[M]; int main() { while(scanf("%d",&n),n) { p[0]=p[n+1]=max=-1; for(i=1;i<=n;i++) { l[i]=r[i]=i; scanf("%lld",&p[i]); } for(i=n;i>=1;i--)while(p[i]<=p[r[i]+1])r[i]=r[r[i]+1]; for(i=1;i<=n;i++)while(p[l[i]-1]>=p[i])l[i]=l[l[i]-1]; for(i=1;i<=n;i++) { term=p[i]*(r[i]-l[i]+1); if(max<term)max=term; } printf("%lld\n",max); } return(0); }
相关推荐
在LeetCode这个著名的在线编程挑战平台上,有一道题目名为“Largest Rectangle in Histogram”(直译为“柱状图中的最大矩形”),这是一道与数据结构和算法密切相关的题目,旨在考察程序员对栈、动态规划以及几何...
LintCode - 122. Largest Rectangle in Histogram(直方图最大矩形覆盖)(单调栈)题目链接题目解析主要是运用单调栈(单
python python_leetcode题解之084_Largest_Rectangle_in_Histogram
javascript js_leetcode题解之84-largest-rectangle-in-histogram.js
c c语言_leetcode题解之0084_largest_rectangle_in_histogram.zip
http://blog.csdn.net/two_water/article/details/53004027 LeetCode_直方图最大面积(Largest Rectangle in Histogram)
在C++编程语言中,矩形面积的计算是一项基础但重要的任务,特别是在计算机科学的教育初期,这样的练习有助于学生理解面向对象编程的概念。本实验题目是"Rectangle 矩形求面积",这是北邮C++实验课程的一个作业,旨在...
总的来说,"求矩形面积的源程序"这个主题涵盖了基本的编程概念,包括类的设计、属性的使用、方法的定义以及条件判断。通过学习这些基础知识,开发者可以为更复杂的几何图形或数学问题编写类似的程序。
1,求直方图中最大矩形的面积。 上面是一个直方图,其中每个条的宽度为 1,给定高度 = [2,1,5,6,2,3]。 最大的矩形显示在阴影区域中,其面积 = 10 单位。 实现 1:O(n^2) class Solution { public int ...
实现利用Rectangle输出一个矩形的周长和面积
总结一下,创建一个名为`rectangle`的矩形类,其属性数据为矩形左上角和右上角的点的坐标,能够计算矩形的面积,是通过定义`Point`类来表示坐标点,然后在`Rectangle`类中存储这两个点,并实现`area`方法来计算面积...
// 计算矩形面积的方法 public double area() { return (double) width * height; } // 计算矩形周长的方法 public double perimeter() { return 2 * (width + height); } // 获取矩形宽度的方法 public...
java 实验 继承与多态rectAngle 定义矩形类,用户输入矩形的长与宽,程序计算其面积和周长;派生子类正方形类,定义一个接口Printable源代码
【计算矩形面积小程序】是一个非常适合非计算机专业学生进行课程设计的项目,它涉及到基本的编程概念和面向对象的设计思想。这个小程序的核心是通过创建一个“面积类”来实现矩形面积的计算,并且能够处理多个矩形的...
12. 栈的最大矩形面积(Largest Rectangle in Histogram):给定一个数组,找到栈中的最大矩形面积。难度:困难 13.柱状图中最大的矩形(Largest Rectangle in Histogram):给定一个数组,找到柱状图中的最大矩形...
在"简单类的调用,求矩形梯形面积"这个主题中,我们将探讨如何利用面向对象编程(OOP)的概念来计算这两种几何形状的面积。 首先,我们需要为矩形和梯形创建各自的类。在Java中,一个类定义了对象的属性(数据成员...
3. 实现一个函数,计算两个矩形的求差结果,生成新的`Rectangle`对象。 4. 在每次矩形发生变化时,更新可视化界面,重新绘制所有矩形。 5. 可以使用`Inflate`方法来扩展或收缩矩形,`IntersectsWith`方法检测两个...
1.建立一个形状类Shape作为基类,派生出圆类Circle和矩形类Rectangle,求出面积并获取相关信息。 具体要求如下: (1)形状类Shape (a)保护数据成员 double x,y:对于不同的形状,x和y表示不同的含义,如对于圆,...
需要注意的是,矩形面积为`(right - left) * (top - bottom)`,但这里的重叠矩形可能会导致负面积,因此需要进行适当的检查: ```cpp int overlapArea(const Rectangle &rect1, const Rectangle &rect2) { ...