题目链接:http://poj.org/problem?id=2082
/*
栈的运用:栈是一种先进后出的数据结构,通过一个栈顶指针对入栈和出栈控制,可以改变一个序列的顺序本题就是运用栈将每个矩形(包括新形成的矩形)入栈,并且栈中矩形的高度成递增趋势,这样扫描整个栈就可以得出答案了
*/
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
struct rect
{
int w;
int h;
}data;
int main()
{
int n,ans,i,lasth,totalw,area;
while(scanf("%d",&n)&&n!=-1)
{
ans=0;
stack<rect>s; //定义一个空栈
lasth=0; //将初始高度设为0
for(i=0;i<n;i++)
{
scanf("%d%d",&data.w,&data.h);
if(data.h>=lasth)//如果将要入栈的矩形高度大于等于上一个矩形的高度则入栈
{
s.push(data);
}
else
{
totalw=0;
area=0;
while(!s.empty()&&s.top().h>data.h)/*不能入栈的矩形与栈中矩形组成一个新的最大面积的矩形,
然后将新的矩形入栈,被组合的矩形要出栈
*/
{
totalw+=s.top().w;
area=totalw*s.top().h;
if(area>ans)
{
ans=area;
}
s.pop();
}
totalw+=data.w;
data.w=totalw;
s.push(data);
}
lasth=data.h;
}
totalw=0;
area=0;
while(!s.empty())//将整个栈扫描一遍 得出最大面积的矩形
{
totalw+=s.top().w;
area=totalw*s.top().h;
if(area>ans)
ans=area;
s.pop();
}
printf("%d\n",ans);
}
return 0;
}