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