本题链接:最大的矩形
本博客给出本题截图:
C++
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int h[N];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i ++ )
cin >> h[i];
int res = 0;
for (int i = 1; i <= n; i ++ )
{
int l = i, r = i;
while (l >= 1 && h[l] >= h[i]) l --;
while (r <= n && h[r] >= h[i]) r ++;
res = max(res, h[i] * (r - l - 1));
}
cout << res << endl;
return 0;
}
总结
思路:对于每一个矩形,求出它左侧最远可以扩展到哪里,右侧最远可以最大扩展到哪里,计算出面积,最后所有的矩形的面积取max