Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 2747 | Accepted: 1389 |
Descriptionios
Inputapp
Outputthis
Sample Inputspa
3 1 2 3 4 1 2 3 3 4 1 2 3 4 -1
Sample Outputcode
12 14
題目大意:給出一系列矩形的寬度和高度,矩形沿着x軸對齊,求這些矩形組成的連續矩形區域的最大面積。
解題方法:這是一道很是好的題,用棧保存矩形,若是高度遞增則不斷入棧,若是遇到當前輸入的比棧頂高度小,則從棧頂開始不斷出棧而且計算最大面積,直到棧頂高度小於當前輸入高度則中止出棧,並把開始出棧矩形的寬度累加獲得totalw,把totalw和當前輸入的矩形寬度相加獲得當前輸入矩形的寬度,併入棧,這樣棧中保存的永遠都是高度遞增的矩形,最後輸入完了以後若是棧不爲空,則依次出棧並計算最大面積。
#include <stdio.h> #include <iostream> #include <string.h> #include <stack> using namespace std; typedef struct { int w; int h; }Node; int main() { stack<Node> Stack; int totalw, ans, w, h, n; while(scanf("%d", &n) != EOF && n != -1) { ans = 0; for (int i = 0; i < n; i++) { scanf("%d%d", &w, &h); if (Stack.empty())//若是棧爲空,則入棧 { Node temp; temp.w = w; temp.h = h; Stack.push(temp); } else { totalw = 0; if (h >= Stack.top().h)//若是當前矩形高度大於棧頂矩形高度,入棧 { Node temp; temp.w = w; temp.h = h; Stack.push(temp); } else { //若是當前輸入矩形高度小於棧頂矩形高度,出棧並計算最大面積 while(!Stack.empty() && Stack.top().h > h) { //寬度從棧頂開始依次累加 totalw += Stack.top().w; if (ans < totalw * Stack.top().h) { //獲得最大面積 ans = totalw * Stack.top().h; } Stack.pop(); } Node temp; //出棧完畢以後,棧爲空或者棧頂矩形高度小於當前輸入高度, //以保證棧中的矩形高度遞增 temp.w = w + totalw;//加上開始出棧的全部矩形寬度之和,即爲當前輸入矩形的寬度 temp.h = h; Stack.push(temp); } } } totalw = 0; //若是棧不爲空,則依次出棧並計算最大面積 while(!Stack.empty()) { totalw += Stack.top().w; if (ans < totalw * Stack.top().h) { ans = totalw * Stack.top().h; } Stack.pop(); } printf("%d\n", ans); } return 0; }