题意大概讲述的是有许多宽度为1的矩形,问你最大面积的矩形是多少。
更具模型可以得知,要是有一个高度较小块进入后,无法给前面高度较高的块增加同一高度的面积。这时候就可以处理前面的高块,因为现在的块是较低的,已经没办法可以接到后面较高的。所以删去前面所有比当前块高的块,并且处理这些块可以得到的面积。最后一次性处理所有剩下的块。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
typedef long long LL;
const int maxx = 100005;
struct node
{
LL h, w;
}stack[maxx];
int top;
int main()
{
while (~scanf("%d", &n) && n)
{
top = 0;
LL ans = 0;
int now;
scanf("%d", &now);
stack[top].h = now;
stack[top++].w = 1;
for (int i = 2; i <= n; i++)
{
scanf("%d", &now);
if (now >= stack[top - 1].h)
{
stack[top].h = now;
stack[top++].w = 1;
}
else
{
int len = 0;
while (top - 1 >= 0 && now < stack[top - 1].h)
{
LL tmp = stack[top - 1].h*(stack[top - 1].w + len);
ans = max(ans, tmp);
len += stack[top - 1].w;
top--;
}
stack[top].h = now;
stack[top++].w = 1 + len;
}
}
int len = 0;
while (top - 1 >= 0)
{
LL tmp = stack[top - 1].h*(stack[top - 1].w + len);
ans = max(ans, tmp);
len += stack[top - 1].w;
top--;
}
printf("%lld\n", ans);
}
return 0;
}
本人愚见,如有不足,欢迎指点。