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. EasyUI实战经验总结,给有需要的人

    最近公司培训EasyUI,就做下总结吧,给有需要的人. 1.最常用的表格 <div class="easyui-panel" data-options="regio ...

  2. #Linux学习笔记# Linux在线帮助文档man page

    man是manual的简称,表示手册页.Linux系统的帮助手册页是按照章节(SECTION)来存储了.man手册页分为下面几个章节. 1 普通命令 2 内核提供的系统调用 3 库调用(C库函数) 4 ...

  3. 每天一个linux命令(32):wc命令

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  4. 每天一个linux命令(17):locate 命令

    locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...

  5. Excel如何查找名字重复的数据

    来源于:http://jingyan.baidu.com/article/414eccf6091ff86b431f0aec.html Cokery今天在帮助同事整理数据的时候遇到了一个难题,就是在Ex ...

  6. Shell重定向&>file、2>&1、1>&2的区别

    shell上: 0表示标准输入 1表示标准输出 2表示标准错误输出 > 默认为标准输出重定向,与 1> 相同 2>&1 意思是把 标准错误输出 重定向到 标准输出. & ...

  7. mac os x常用快捷键及用法

    最近在研究mac os x系统,开始入手,很不习惯,和windows差别很大,毕竟unix内核.使用中总结了一些使用快捷键(默认),持续更新,欢迎大家补充.1.撤销:command+z 保存:comm ...

  8. Java编程思想学习(六) 多态

    1.Java语言的三大特性:继承.封装和多态. 继承:复用类的一种方法,可以简省很多代码: 封装:通过合并特征和行为来创建新的数据类型.[这种“数据类型”跟Java本身提供的8大“基本数据类型”的地位 ...

  9. BZOJ-1202 狡猾的商人 并查集+前缀和

    我记得这个题,上次之前做的时候没改完,撂下了,今天突然想改发现,woc肿么A 了= =看来是我记错了.. 1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec Memory ...

  10. CSS文字排版

    一.font-size 我来试一试:为第一段中的“胆小如鼠”设置字号为:20px,字体颜色为:red. <!DOCTYPE HTML> <html> <head> ...