【POJ】2082

来源:互联网 发布:博优化纤 编辑:程序博客网 时间:2024/06/06 11:01

http://poj.org/problem?id=2082

给一些矩形的宽和高,矩形沿着x轴对齐,求这些矩形组成的连续矩形区域的最大面积。

(1)用栈保存矩形,若高度递增则不断入栈;若当前矩形比栈顶矩形矮,则不断出栈并计算最大面积,直至栈顶矩形比当前矩形矮。
(2)把出栈矩形宽累加得到W,将W和当前矩形宽相加得到当前输入矩形的宽度,入栈。
(3)输入完后若栈不为空,则依次出栈计算最大面积。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <vector>#include <numeric>#include <algorithm>using namespace std;struct node{    int w,h;};stack <node> s;int n,ans,W;int main(){    while (cin >> n && n!=-1){        ans=0;        node tmp;        for (int i=0;i<n;i++){            scanf("%d%d",&tmp.w,&tmp.h);            if (s.empty()){                s.push(tmp);            }            else{                W=0;                while (!s.empty()&&s.top().h>tmp.h){                    W+=s.top().w;                    ans=max(ans,W*s.top().h);                    s.pop();                }                tmp.w+=W;                s.push(tmp);            }        }        W=0;        while (!s.empty()){            W+=s.top().w;            ans=max(ans,W*s.top().h);            s.pop();        }        cout << ans << endl;    }}
原创粉丝点击