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.
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.
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
8 4000
Huge input, scanf is recommended.
题目大意:
在柱状图中找到一个面积最大的矩阵并输出其面积。
解题思路:
用栈去维护一个递增的序列, 栈中存的是每一个柱形的下标, 遇到比栈顶元素小的就弹栈并计算出从此个柱形能扩展的最大面积是多少。注意数据大小, 面积的计算用long long。
代码:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
using namespace std;
/*
*ios::sync_with_stdio(false);
*/
*ios::sync_with_stdio(false);
*/
typedef long long ll;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = (int) 1e5 + 9;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = (int) 1e5 + 9;
int h[Max];
int main() {
// definition
int n;
long long ans;
// initialization
while (scanf("%d", &n) && n) {
for (int i = 1; i <= n; ++i) {
scanf("%d", &h[i]);
}
stack < int> S;
// prevent overfull
S.push(0); h[++n] = 0; ans = 0;
for (int i = 1; i <= n; ++i) {
while (h[i] < h[S.top()]) {
// length and width
long long l = h[S.top()]; S.pop();
long long w = i - S.top() - 1;
ans = max(ans, l * w);
}
S.push(i);
}
printf("%lld\n", ans);
}
return 0;
}
int n;
long long ans;
// initialization
while (scanf("%d", &n) && n) {
for (int i = 1; i <= n; ++i) {
scanf("%d", &h[i]);
}
stack < int> S;
// prevent overfull
S.push(0); h[++n] = 0; ans = 0;
for (int i = 1; i <= n; ++i) {
while (h[i] < h[S.top()]) {
// length and width
long long l = h[S.top()]; S.pop();
long long w = i - S.top() - 1;
ans = max(ans, l * w);
}
S.push(i);
}
printf("%lld\n", ans);
}
return 0;
}