题目地址:http://poj.org/problem?id=2559

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

如果确定了长方形的左端点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 = 0;
int t = 0;
int i;
for (i = 0; i < n; ++i){
while (t > 0 && h[stack[t-1]] >= h[i]) t--;
L[i] = (t == 0) ? 0 : (stack[t-1] + 1);
stack[t++] = i;
} //计算R
t = 0;
for (i = n - 1; i >= 0; --i){
while (t > 0 && h[stack[t-1]] >= h[i]) t--;
R[i] = (t == 0) ? n : stack[t-1];
stack[t++] = i;
} for (i = 0; i < n; ++i){
ans = max(ans, (long long)h[i] * (R[i] - L[i]));
}
printf("%lld\n", ans);
} int main(void){
int i;
while (scanf("%d", &n) != EOF && n != 0){
for (i = 0; i < n; ++i)
scanf("%d", &h[i]);
solve();
} return 0;
}

参考资料:挑战程序设计竞赛(第2版)

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 (单调栈或者dp)

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

  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

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

  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. android studio 更改背景和设置字体大小

    1,设置字体大小 2,设置背景主题

  2. Css基础-类选择器

    类选择器以一个.显示 <p class="pclass">这是第一个class</p> .pclass { color:red; } <div cla ...

  3. ios基础知识

    1获取系统语言设置 NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults]; NSArray *languages = ...

  4. 2015北京网络赛 J Clarke and puzzle 求五维偏序 分块+bitset

    Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc20 ...

  5. iOS开发——高级技术&生成二维码

      生成二维码 因为项目里需要新增个功能,该功能用到了二维码技术.于是我便查阅了资料,先学习了二维码的生成. 我们使用libqrencode库来生成二维码.下载地址http://download.cs ...

  6. CSS:表格样式(设置表格边框/文字/背景的样式)

    使用CSS能够制作出十分精美的表格. 代码整理自w3school:http://www.w3school.com.cn 效果图: 代码: <!DOCTYPE html PUBLIC " ...

  7. QT: QByteArray储存二进制数据(包括结构体,自定义QT对象)

      因为利用QByteArray可以很方便的利用其API对内存数据进行访问和修改, 构建数据库blob字段时必不可少; 那如何向blob内写入自定义的结构体和类 1. 利用memcpy拷贝内存数据 / ...

  8. IIS 之 失败请求跟踪规则

    若想使用此功能需先启动如下图的Windows功能: 利用失败请求跟踪功能,可以在出现问题时捕获相应的XML格式的日志,从而无需重现该问题即可开始故障排除.此外,还可以定义应用程序的失败条件并配置要基于 ...

  9. Lucene 搜索功能

    搜索过程 图解: 主要 API: IndexSearcher:    //所有搜索都通过 IndexSearcher 进行,他们将调用该类中重载的 search() 方法 Query:         ...

  10. 关于运行SWT程序遇到的一个错误的总结

    具体的错误信息如下: Exception in thread "main" java.lang.SecurityException: SHA1 digest error for o ...