Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19689 Accepted: 6370
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.
题意:
有一堆宽度恒为1、高度不定的长方形排成一列,找出最大面积的长方形。
思路:
单调栈。维护一个高度单调递增的栈(没有相等项),对于每一次进栈,判断其元素高度是否大于栈顶元素:若大于,入栈;若小于,则弹出栈顶元素,每次弹出计算面积,覆盖最大值。输入完毕后得到单调栈,从栈顶开始逐个出栈,每次出栈计算面积,覆盖最大值。最终输出最大值。
//使用stack
#include<cstdio>
#include<stack>
using namespace std;
struct Node {
int height;
int width;
Node(int h = 0, int w = 0):height(h), width(w) {};
};
stack<Node> st;
long long ans, cur;
long long width;
int height;
int n;
int main() {
while (scanf("%d", &n)!=EOF && n) {
ans = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &height);
width = 0;
while (!st.empty() && st.top().height >= height) {
cur = st.top().height * (st.top().width + width);
if (cur>ans)
ans = cur;
width += st.top().width;
st.pop();
}
st.push(Node(height, width + 1));
}
width = 0;
while (!st.empty()) {
cur = st.top().height * (st.top().width + width);
if (cur > ans)
ans = cur;
width += st.top().width;
st.pop();
}
printf("%lld\n", ans);
}
}
//使用数组
#include<cstdio>
using namespace std;
const int maxn = 100000;
struct Node {
int height;
int width;
Node(int h = 0, int w = 0) :height(h), width(w) { };
}st[maxn + 10];
int top;
int main() {
long long ans, cur;
long long width;
int height;
int n;
while (scanf("%d", &n) != EOF && n) {
top = 0;
ans = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &height);
width = 0;
// 出栈
while (top>0 && st[top - 1].height >= height) {
cur = st[top - 1].height * (st[top - 1].width + width);
if (cur > ans)
ans = cur;
width += st[top - 1].width;
--top;
}
// 进栈
st[top].height = height;
st[top].width = width + 1;
++top;
}
// 在栈中找最大
width = 0;
while (top > 0) {
cur = st[top - 1].height * (st[top - 1].width + width);
if (cur > ans)
ans = cur;
width += st[top - 1].width;
--top;
}
printf("%lld\n", ans);
}
return 0;
}