Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18942   Accepted: 6083

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

Source

刚开始竟然没有注意到区间长度的累计,各种算错。

维护一个单增的单调栈,每步更新区间长度和答案即可。

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
struct stk{
long long h,p;
}st[mxn];
int top;
int n;
long long ans;
int main(){
while(scanf("%d",&n) && n){
ans=;
int i,j;
int num;
for(i=;i<=n+;i++){
if(i>n)num=;//数据读完后清栈
else scanf("%I64d",&num);
if(num>=st[top].h) st[++top].h=num,st[top].p=;//计算新区间
else{
int len=;
while(top && num<=st[top].h){
len+=st[top].p;
ans=max(ans,len*st[top].h);
top--;
}
st[++top].p=len+;//累计长度
st[top].h=num;//更新高度
}
}
printf("%I64d\n",ans);
}
return ;
}

POJ 2559 Largest Rectangle in a Histogram的更多相关文章

  1. [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

    [POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...

  2. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

  3. stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

    题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...

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

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  5. POJ 2559 Largest Rectangle in a Histogram -- 动态规划

    题目地址:http://poj.org/problem?id=2559 Description A histogram is a polygon composed of a sequence of r ...

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

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

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

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

  8. POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

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

  9. 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)

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

随机推荐

  1. maven总结1

     环境:win7   maven版本:apache-maven-3.1.1-bin.zip maven安装 1.确定已经正确安装jdk,若未安装需要先安装jdk 2.http://maven.apac ...

  2. java 21 - 6 字符缓冲流的特殊方法以及该方法高效复制文件

    字符缓冲流的特殊方法: A.BufferedWriter: public void newLine():根据系统来决定换行符 private static void write() throws IO ...

  3. java11-6 String类的其它功能

    String类的其他功能: 替换功能: String replace(char old,char new) String replace(String old,String new) 去除字符串两空格 ...

  4. 上传图片shell绕过过滤的方法

    一般网站图片上传功能都对文件进行过滤,防止webshelll写入.但不同的程序对过滤也不一样,如何突破过滤继续上传? 本文总结了七种方法,可以突破! .文件头+GIF89a法.(php)//这个很好理 ...

  5. center

    center标签对其包围的文本进行水平居中处理

  6. Managing the Lifecycle of a Service

    service的生命周期,从它被创建开始,到它被销毁为止,可以有两条不同的路径: A started service 被开启的service通过其他组件调用 startService()被创建. 这种 ...

  7. swift 判断输入的字符串是否为数字

    // 判断输入的字符串是否为数字,不含其它字符 func isPurnInt(string: String) -> Bool { let scan: Scanner = Scanner(stri ...

  8. ls -F一种非常有用的ls格式

    ls -F一种非常有用的ls格式  tz/y/yupeng > ls -F#q#           News/         doc/          images/       mbox ...

  9. VMware 不可恢复错误(svga)”解决方法

    虚拟机VMware 文件在迁移到另一台计算机时出现"VMware Workstation 不可恢复错误(svga)"  将另一台机器的 VMware 文件拷贝至本机,打开虚拟机出现 ...

  10. (转载)Apache下error.log文件太大的处理

    偶尔发现Apache下的错误日志非常的大,有5G多,先停止Apache服务的所有进程,最简单就是输命令:net stop apache2.4,然后删除 Apache/logs/目录下的 error.l ...