Largest Rectangle in a Histogram
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21204   Accepted: 6831

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.

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 nintegers 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.

Source

 
 
 
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<stack>
using namespace std; struct node
{
int h,id;
node(int a,int b){ h=a; id=b; } //h存高度,id存第几块
};
int n;
stack<node> Q;
long long a[],l[],r[];
//在a[i]的高度下,能达到的左边界l[i],能达到的右边界r[i] int main()
{
while(~scanf("%d",&n))
{
if(n==) break;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
while(!Q.empty()) Q.pop(); //清空栈Q
memset(l,,sizeof(l));
memset(r,,sizeof(r));
//求l[]数组,O(n)复杂度找左边界
for(int i=;i<=n;i++)
{
if (Q.empty()) {l[i]=; Q.push(node(a[i],i)); continue; }
node u=Q.top();
if (u.h<a[i]) {l[i]=i; Q.push(node(a[i],i)); continue;}
while(u.h>=a[i]) //如果当前高度a[i],比栈顶元素低,则栈顶元素能到达的左边界也能到达。
{
l[i]=l[u.id];
Q.pop();
if (Q.empty()) break;
u=Q.top();
}
Q.push(node(a[i],i) );
} //求r[]数组,扫右边界
while(!Q.empty()) Q.pop();
for(int i=n;i>=;i--)
{
if (Q.empty()) {r[i]=i; Q.push(node(a[i],i)); continue; }
node u=Q.top();
if (u.h<a[i]) {r[i]=i; Q.push(node(a[i],i));continue;}
while(u.h>=a[i])
{
r[i]=r[u.id];
Q.pop();
if (Q.empty()) break;
u=Q.top();
}
Q.push(node(a[i],i) );
} long long sum=;
for(int i=;i<=n;i++)
sum=max(sum,a[i]*(r[i]-l[i]+));
printf("%lld\n",sum);
}
return ;
}

ZOJ 1985 Largest Rectangle in a Histogram(刷广告)2010辽宁省赛的更多相关文章

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

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

  2. 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 ...

  3. Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K ...

  4. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  5. Largest Rectangle in a Histogram(HDU1506)

    Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...

  6. POJ 2559 Largest Rectangle in a Histogram

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18942   Accepted: 6083 Description A hi ...

  7. Largest Rectangle in a Histogram

    2107: Largest Rectangle in a Histogram Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 777  Solved: 22 ...

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

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

  9. hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)

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

随机推荐

  1. codeforces 350 div2 D Magic Powder - 2 二分

    D2. Magic Powder - 2 time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. poj 1523 SPF 无向图求割点

    SPF Description Consider the two networks shown below. Assuming that data moves around these network ...

  3. c++ 多继承 public

    以下代码会报错 #include <iostream> using namespace std; class Sofa { public: Sofa(); ~Sofa(); void si ...

  4. shell 余弦值转角度

    范例:余弦值转角度 用 bc -l 计算,可以获得高精度: $ export cos=0.996293; echo "scale=100; a(sqrt(1-$cos^2)/$cos)*18 ...

  5. VS 常见快捷键有哪些

    自动对齐点[编辑]-[高级]-[设置选定内容的格式]或者按Ctrl + K 然后再按Ctrl + F 就好了 你可以在常用快捷键自定义 窗口中进行查看1.进入工具-选项 对话框2.选择[环境]-[键盘 ...

  6. Java8 新特性之默认接口方法

    摘要: 从java8开始,接口不只是一个只能声明方法的地方,我们还可以在声明方法时,给方法一个默认的实现,我们称之为默认接口方法,这样所有实现该接口的子类都可以持有该方法的默认实现. · 待定 一. ...

  7. cookie session localstorage sessionStorage区别

    cookie:http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html 重要特点: 1.cookie 有大小设置,有过期时间设 ...

  8. Style、ControlTemplate 和 DataTemplate 触发器

    本文摘要:    1:属性触发器:    2:数据触发器:    3:事件触发器: Style.ControlTemplate 和 DataTemplate 都有触发器集合.    属性触发器只检查W ...

  9. mac外接显示器 双屏同时滑动问题

    问题:mac pro 外接一个显示器,用四个手指横向切换屏幕的时候,外接的显示器也一起跟着动了   解决:      

  10. 20170814xlVBA部分代号收盘价转置

    原始数据: 转置效果: Sub TransformData() Dim Rng As Range Dim Arr As Variant Dim Dic As Object Dim dCode As O ...