POJ 2082 最大矩形面积

#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
struct node{
    int w,h;
    node(int _w,int _h):w(_w),h(_h){}
};
int n,w,h;
int main()
{
    while(scanf("%d",&n)&&(n+1))
    {
        int ans=0;
        stack <node> s;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&w,&h);
            if(s.empty()||h>s.top().h)
                s.push(node(w,h));
            else
            {
                int total=0,square=0;
                while(!s.empty())
                {
                    if(h<s.top().h)
                    {
                        total+=s.top().w;
                        square=total*s.top().h;
                        ans=ans<square?square:ans;
                        s.pop();
                    }
                    else
                        break;
                }
                w+=total;
                s.push(node(w,h));
            }
        }
        int total=0,square=0;
        while(!s.empty())
        {
            total+=s.top().w;
            square=total*s.top().h;
            ans=ans<square?square:ans;
            s.pop();
        }
        cout<<ans<<endl;
    }
    return 0;
}

posted on 2015-03-18 12:24  一锅土豆  阅读(110)  评论(0编辑  收藏  举报