2107: Largest Rectangle in a Histogram

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 777  Solved: 220

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

HINT

Huge input, scanf is recommended.

如果确定了长方形的左端点L和右端点R,那么最大可能的高度就是min{hi|L <= i < R}。

L[i] = (j <= i并且h[j-1] < h[i]的最大的j)

R[i] = (j > i并且h[j] > h[i]的最小的j)

 #include <stdio.h>
#define MAX_N 100000 int n;
int h[MAX_N];
int L[MAX_N], R[MAX_N];
int stack[MAX_N]; long long max(long long a, long long b)
{
return (a > b) ? a : b;
} void solve()
{
//计算L
long long ans = ;
int t = ;
int i;
for (i = ; i < n; ++i)
{
while (t > && h[stack[t-]] >= h[i])
t--;
L[i] = (t == ) ? : (stack[t-] + );
stack[t++] = i;
} //计算R
t = ;
for (i = n - ; i >= ; --i)
{
while (t > && h[stack[t-]] >= h[i])
t--;
R[i] = (t == ) ? n : stack[t-];
stack[t++] = i;
} for (i = ; i < n; ++i)
{
ans=max ( ans, ( long long)h[i]*( R[i]- L[i]));
}
printf("%lld\n", ans);
} int main(void){
// freopen("a.txt","r",stdin);
int i;
while (scanf("%d", &n) != EOF && n != )
{
for (i = ; i < n; ++i)
scanf("%d", &h[i]);
solve();
} return ;
}

Acknowledge:jdplus     http://blog.csdn.net/jdplus/article/details/20606673

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

  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. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

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

  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. HDU 1506 Largest Rectangle in a Histogram set+二分

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

随机推荐

  1. Windows Phone & Windows App应用程序崩溃crash信息抓取方法

    最近有用户反馈,应用有崩溃的情况,可是本地调试却无法重现问题,理所当然的,我想到了微软的开发者仪表盘,可以查看一段时间内的carsh记录,不过仪表盘生成carsh记录不是实时的,而且生成的报告查看非常 ...

  2. ORA-00911: 无效字符

    思路:遇到这样问题首先第一步:将有误sql粘至数据库运行一下,如果报错,说明sql存在问题. 第二步:数据库没问题.那么就要想你的书写方式是否正确,是否是ibatasi里的写法,或许是多了个 :  或 ...

  3. [USACO2005][POJ3045]Cow Acrobats(贪心)

    题目:http://poj.org/problem?id=3045 题意:每个牛都有一个wi和si,试将他们排序,每头牛的风险值等于前面所有牛的wj(j<i)之和-si,求风险值最大的牛的最小风 ...

  4. Qt无边框,可移动窗口

    QPoint dragPosition; void MainWindow::mousePressEvent(QMouseEvent *event) { if(event->button()==Q ...

  5. JSON.NET 简单的使用

    JSON.NET(http://json.codeplex.com/)使用来将.NET中的对象转换为JSON字符串(序列化?),或者将JSON字符串转换为.NET中已有类型的对象(反序列化?) 首先为 ...

  6. C#中async/await中的异常处理

    在同步编程中,一旦出现错误就会抛出异常,我们可以使用try-catch来捕捉异常,而未被捕获的异常则会不断向上传递,形成一个简单而统一的错误处理机制.不过对于异步编程来说,异常处理一直是件麻烦的事情, ...

  7. JS所谓的享元模式-->

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  8. AMD&CMD

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Java sun的JDK

    JDK概述 JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品.自从Java推出以来,JDK已经成为使用最广泛的Java SDK(Softwa ...

  10. JSON前端页面解析

    JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言 * JSON 具有自我描述性,更 ...