POJ 2559 Largest Rectangle in a Histogram
2016-07-03 22:46
288 查看
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.
【题目分析】
单调栈进阶了。需要用long long就可了。
【代码】
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.
【题目分析】
单调栈进阶了。需要用long long就可了。
【代码】
#include <cstdio> using namespace std; const int MAX_N = 100005; int deep[MAX_N], stack[MAX_N], A[MAX_N], n; int main() { while (scanf("%d", &n) && n) { for (int i = 1; i <= n; i++) scanf("%d", &A[i]); A[n+1] = 0; n++; stack[0] = 0; long long ans = 0; deep[0] = 0; int top = 1; for (int i = 1; i <= n; i++) { int depth = i; while (A[i] < stack[top-1]) { long long tmp = (long long)(i - deep[top-1]) * (long long)stack[top-1]; if (tmp > ans) ans = tmp; depth = deep[top-1]; top --; } if (A[i] > stack[top-1]) { stack[top] = A[i]; deep[top] = depth; top ++; } } printf("%lld\n", ans); } return 0; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2159 Ancient Cipher
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- [数论]poj2635__The Embarrassed Cryptographer
- [二分图匹配]poj2446__Chessboard
- POJ1050 最大子矩阵和
- 用单调栈解决最大连续矩形面积问题