考古学家在人迹罕至的一块平地上发现了由一堆木板拼成的墙。令人惊奇的是这些木板的宽度都相同!地下的部分都已腐烂,而地上的部分也有高有低,甚至有的地方根本没有木板,所以考古学家决定带走面积最大的长方形回去研究。
输入:
首先是整数n(1<=n<=100000),表示木板的块数。接下来是n个整数h1,...,hn, 其中0<=hi<=1000000000,它们按照从左到右的顺序表示木板的高度。每块木板的宽度都是1。
最后一个0表示程序的结束。
输出:
其中最大长方形的面积。
样例
输入(1)
3 2000 2000 2000
7 1 2 5 6 1 3 3
0
输出(1)
6000
10
代码
#include <iostream>
#include <stack>
using namespace std;
struct Node {
int height;
int pre;
int pos;
};
int main() {
int board_num;
stack<Node> St;
while (cin >> board_num && board_num) {
long long sum_area = 0;
for (int i = 1; i <= board_num; i++) {
int temp_height;
int pre = 0;
cin >> temp_height;
while (!St.empty() && St.top().height > temp_height) {
Node temp = St.top();
St.pop();
pre += temp.pre + 1;
sum_area = max(sum_area, static_cast<long long>(temp.height) * (i - temp.pos + temp.pre));
}
Node push_node{temp_height, pre, i};
St.push(push_node);
}
while (!St.empty()) {
Node temp = St.top();
St.pop();
sum_area = max(sum_area, static_cast<long long>(temp.height) * (board_num + 1 - temp.pos + temp.pre));
}
cout << sum_area << endl;
}
return 0;
}