Terrible Sets(求矩形的最大面积(递增栈))

题意:紧贴x轴有一些相互挨着的矩形,给定每个矩形的长宽,问他们可以形成的最大矩形是多少。

Sample Input

3
1 2
3 4
1 2
3
3 4
1 2
3 4
-1

Sample Output

12
14
 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
",ans);
60     }
61 }
原文地址:https://www.cnblogs.com/xiaoying1245970347/p/4704134.html