Terrible Sets(求矩形的最大面積(遞增棧))

來源:互聯網
上載者:User

標籤:

題意:緊貼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(求矩形的最大面積(遞增棧))

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.