Problem Description
在ACdream王国中有一排由N个矩形房子组成的住宅区,每个房子高为h[i],小Z想知道,在这些矩形房子中,你能找到的最大矩形面积是多少呢?
Input
多组数据,对于每组数据,首先是一个整数N,表示房子的数目(1<=N<=100000)
接下来是一行N个整数h[i],表示这些矩形的高度(1<=h[i]<=1000000)
Output
对于每组数据,输出一个整数,表示最大的矩形面积。
Sample Input
5
1 2 3 4 5
7
2 1 4 5 1 3 3
4
1000 1000 1000 1000
Sample Output
9
8
4000
Hint
样例一如图:
就是找以这个为中心,比他高的左右边界,遍历两次。然后再遍历一次找最大值。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 100005;
ll h[maxn],l[maxn],r[maxn];
int main()
{
#ifdef LOCAL
freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
#endif // LOCAL
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i = 1;i <= n;i++)scanf("%lld",&h[i]);
for(int i = 1;i <= n;i++)
{
l[i] = i;r[i] = i;
}
for(int i = 2;i <= n;i++)
{
if(h[i] > h[i - 1])
l[i] = i;
else
{
int temp = l[i - 1];
while(temp >= 1&&h[temp - 1] >= h[i])
{
temp = l[temp - 1];
}
l[i] = temp;
}
}
for(int i = n - 1;i >= 1;i--)
{
if(h[i] > h[i + 1])
r[i] = i;
else
{
int temp = r[i + 1];
while(temp <= n&&h[temp + 1] >= h[i])
{
temp = r[temp + 1];
}
r[i] = temp;
}
}
ll ans = 0;
for(int i = 1;i <= n;i++)
{
ans = max(ans,h[i]*(r[i] - l[i] + 1));
}
printf("%lld\n",ans);
}
return 0;
}