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. sphinx全文检索之PHP使用教程

    以上一篇的email数据表为例: 数据结构: 01.CREATE TABLE email ( 02.emailid mediumint(8) unsigned NOT NULL auto_increm ...

  2. iPhone 5,6,6 plus 尺寸

  3. eclispe远程调试tomcat

    在eclispe中新建web应用,名字叫webtest.里面只有一个HelloServlet.Web.xml配置如下. 修改tomcat的启动脚本startup.bat.复制startup.bat为s ...

  4. WHY IE AGAIN? - string.charAt(x) or string[x]?

    近期今天在写一个"删除字符串中反复字符串"的函数,代码例如以下: 开门见山,重点 string.charAt(index) 取代 string[index] function re ...

  5. Citrix 服务器虚拟化之九 Xenserver虚拟机的XenMotion

    Citrix 服务器虚拟化之九 Xenserver虚拟机的XenMotion XenMotion 是 XenServer 的一项功能,能够将正在运行的虚拟机从一台 XenServer 主机上迁移到另外 ...

  6. Linux下找不到动态链接库

    1.生成静态库 生成静态库使用ar工具,其实ar是archive的意思 $ar cqs libhello.a hello.o 2.生成动态库 用gcc来完成,由于可能存在多个版本,因此通常指定版本号: ...

  7. 最流行的android组件大全

    目录 [−] 工具和教程 UI组件 类库 游戏引擎 Android HTML5应用 Android 是目前最流行的移动操作系统(还需要加之一吗?). 随着新版本的不断发布, Android的功能也日益 ...

  8. IEquatable(T) interface in .Net

    原文:http://weblogs.asp.net/pawanmishra/iequatable-t-interface-in-net 泛型方法: public static bool AreEqua ...

  9. 实施双工通信框架:SignalR

    SignalR:基于Asp.net平台构建,利用JavaScript或者Websockets,实现在客户端与服务端异步通信的框架. Html5新规范:WebSocket

  10. ADO.NET中连接SQL Sever

    1.在配置文件中定义数据库连接信息. 在配置文件*.config中添加这段代码在<configuration>与</configuration>之间: <connecti ...