#include <iostream>
#include <cstdio>
using namespace std;
const int N = 50005;
struct Elem
{
int height;
int width;
};
Elem stack[N];
int top;
int main()
{
int height, width, n;
int ans, tot, tmp;
while (scanf("%d", &n) != EOF && n != -1)
{
top = 0;
ans = 0;
for (int i = 0; i < n; ++i)
{
scanf("%d%d", &width, &height);
tmp = 0;
while (top > 0 && stack[top - 1].height >= height)
{
tot = stack[top - 1].height * (stack[top - 1].width + tmp);
if (tot > ans) ans = tot;
tmp += stack[top - 1].width;
--top;
}
stack[top].height = height;
stack[top].width = width + tmp;
++top;
}
tmp = 0;
while (top > 0)
{
tot = stack[top - 1].height * (stack[top - 1].width + tmp);
if (tot > ans) ans = tot;
tmp += stack[top - 1].width;
--top;
}
printf("%d\n", ans);
}
return 0;
}
解决这题的关键是保持单调递增的入栈;
一,因为题目给的不可能是单调递增一组数,这时候你要想办法将中间的干掉;
二,比如,假设,所有的矩形z宽为1;有1 3 5 7 4 为高的矩形输入;首先输入1 3 5 7 是没问题的,如果要压入4; 那么必须把前面的5 7 干掉,干掉的同时,你也
要计算被干掉的面积,比如5,7,先弹出7,算出这个矩形面积的值比较,再存入;再就是5,这时候,要注意5,可以把7的合并,可以在上一步记录7的宽度;
弹出5,面积是10;这时候就可以存入4了;
三,最后处理一个单调递增的矩形就简单多了;