题意:题目写的很难读懂。。其实本质是给定n个矩形,将他们并在一起,然后你要在里面尽可能挖一块尽可能大的矩形,求矩形的
面积。
思路:单调栈。我们从左到右扫,维护h单调增。考虑当前第i个矩形,其高为p[i].h,如果比栈顶的h高那么直接将该矩形压栈,不
然,将栈中的点一个个弹出直到栈顶元素的h比p[i].h小。在弹出的时候,初始一个len=0,每次len加上当前弹出元素的w,用
len*h来更新ans,(画图可知这是当前h能画的最大的矩形),弹完元素之后,p[i].w+=len,将p[i]压栈。最后将栈中元素依次弹
出,初始一个len=0,每次len加上当前弹出元素的w,用len*h来更新ans。相见代码:
// file name: poj2082.cpp // // author: kereo // // create time: 2014年11月02日 星期日 20时52分06秒 // //***********************************// #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<set> #include<map> #include<vector> #include<stack> #include<cmath> #include<string> #include<algorithm> using namespace std; typedef long long ll; const int sigma_size=26; const int MAXN=100000+100; const double eps=1e-8; const int inf=0x3fffffff; const int mod=1000000000+7; #define L(x) (x<<1) #define R(x) (x<<1|1) int n; struct node{ int w,h; }p[MAXN]; stack<node>s; int main() { while(~scanf("%d",&n) && n!=-1){ for(int i=1;i<=n;i++) scanf("%d%d",&p[i].w,&p[i].h); int ans=0; for(int i=1;i<=n;i++){ if(s.empty()) s.push(p[i]); else{ node temp=s.top(); if(p[i].h>temp.h) s.push(p[i]); else{ int len=0; while(!s.empty() && s.top().h>=p[i].h){ temp=s.top(); s.pop(); len+=temp.w; ans=max(ans,len*temp.h); } temp.w=len+p[i].w; temp.h=p[i].h; s.push(temp); } } } int len=0; while(!s.empty()){ node temp=s.top(); s.pop(); len+=temp.w; ans=max(ans,len*temp.h); } printf("%d\n",ans); } return 0; }