標籤:
題意:緊貼x軸有一些相互挨著的矩形,給定每個矩形的長寬,問他們可以形成的最大矩形是多少。
Sample Input
31 23 41 233 41 23 4-1
Sample Output
1214
1 #include <iostream> 2 #include <stack> 3 #include <cstdio> 4 5 using namespace std; 6 7 struct rec{ 8 int w; 9 int h;10 11 }data;12 13 int main()14 {15 int n,ans,i,lasth,totalw,curarea;16 17 while(scanf("%d",&n) && n!=-1)18 {19 ans=0;20 stack<rec> s;21 lasth=0;22 23 for(i=0;i<n;i++)24 {25 scanf("%d%d",&data.w,&data.h);26 if(data.h>=lasth)27 s.push(data);28 else{29 30 totalw=0;31 curarea=0;32 33 while(!s.empty()&&s.top().h>data.h)34 {35 totalw+=s.top().w;36 curarea=totalw*s.top().h;37 if(curarea>ans)38 ans=curarea;39 s.pop();40 }41 totalw+=data.w;42 data.w=totalw;43 s.push(data);44 45 }46 lasth=data.h;47 }48 totalw=0;49 curarea=0;50 51 while(!s.empty())52 {53 totalw+=s.top().w;54 curarea=totalw*s.top().h;55 if(curarea>ans)56 ans=curarea;57 s.pop();58 }59 printf("%d\n",ans);60 }61 }
Terrible Sets(求矩形的最大面積(遞增棧))