Largest Rectangle in a Histogram      
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.

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 题意:从左到右有N个高度不同但底边边长为1的矩形,问从中能够裁出的最大面积的矩形的面积是多少。而且矩行的边必须与纵轴、横轴平行。
解析:设置两个数组le[],ri[],分别表示以某个小矩形为起点,向左、向右能延伸的最远位置(比该小矩形的高度小的)用单调栈维护,我写的le[],ri[]是第一个不符合条件的位置,
所以面积=h[i]*(ri[i]-le[i]-1); 代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=;
const double eps=0.00000001;
typedef __int64 LL;
LL H[],le[],ri[],que[]; //高度,左边界,右边界,单调栈
int main()
{
int N;
while(cin>>N)
{
if(!N) break;
for(int i=;i<=N;i++) scanf("%I64d",&H[i]);
int rear=;
for(int st=;st<=N;st++) //从左往右扫,找le[]
{
while(rear>&&H[que[rear]]>=H[st]) --rear; //更新
if(rear==) le[st]=; //如果栈空的话,边界设为0
else le[st]=que[rear];
que[++rear]=st; //入栈
}
rear=;
for(int st=N;st>=;st--) //从右往左扫
{
while(rear>&&H[que[rear]]>=H[st]) --rear;
if(rear==) ri[st]=N+;
else ri[st]=que[rear];
que[++rear]=st;
}
LL ans=;
for(int i=;i<=N;i++) if(ans<H[i]*(ri[i]-le[i]-)) ans=H[i]*(ri[i]-le[i]-); // 答案
cout<<ans<<endl;
}
return ;
}

hdu 1506 Largest Rectangle in a Histogram(单调栈)的更多相关文章

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

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

  2. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  3. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  4. hdu 1506 Largest Rectangle in a Histogram 构造

    题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  5. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  6. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  7. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. HDU 1506 Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

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

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

随机推荐

  1. Android去除系统自带动画的两种方法

    方法一: 在startActivity()或者finish()后紧跟调用: ((Activity) mContext).overridePendingTransition(0, 0); 方法二: 在一 ...

  2. AndroidStudio常见提示

    Required:请求的是String字符串 .     Found:   et.getText()返回的是text.Editable

  3. C#生成带项目编号的Word段落

    using System; using Microsoft.Office.Interop.Word; using Word = Microsoft.Office.Interop.Word; names ...

  4. Laravel-路由-控制器

    (慕课网_轻松学会Laravel-基础篇_天秤vs永恒老师) 一.基础路由 二.多请求路由 三.参数路由 四.路由别名 生成url可以使用别名 五.路由群组 六.路由输出视图 七.控制器参数绑定

  5. Smack+Openfire 接收和发送文件

    转载请注明出处:http://blog.csdn.net/steelychen/article/details/37958839 发送文件须要提供准确的接收放username称(例:user2@192 ...

  6. Network 20Q--Q2 How does Google sell ad spaces?

    在使用Google搜索的时候会发现,搜索出来的页面除了在左边显示搜索结果以外,还会页面的右边推荐一些广告.那么Google是怎么从这些广告挣钱以及广告商可以通过Google广告获得什么利益呢? Goo ...

  7. UVA 1558 - Number Game(博弈dp)

    UVA 1558 - Number Game 题目链接 题意:20之内的数字,每次能够选一个数字,然后它的倍数,还有其它已选数的倍数组合的数都不能再选,谁先不能选数谁就输了,问赢的方法 思路:利用dp ...

  8. Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment handle fo

    标题 Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment hand ...

  9. 未能加载文件或程序集“System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”

    最近用vs2012发布程序,然后将更新后的程序文件部署到服务器上,由于服务器上本来有此系统,所以只更新了修改的文件 . 进行系统登录时提示:未能加载文件或程序集“System.Web.Extensio ...

  10. (转)sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表 的解决办法

    启动SQL Server 2008 Management Studio 工具菜单----选项----Designers(设计器)----阻止保存要求重新创建表的更改  取消勾选即可.