题目链接:http://poj.org/problem?id=2082
题意:给定坐标系上一系列相邻的矩形,高度不等,求把这些矩形连起来看能够得到的最大矩形的面积
思路:利用单调栈,去维护一个高度不下降的矩形,如果遇到一个高度比栈顶低的矩形就出栈,并计算出栈矩形的面积,最后再逐渐计算出最终维护下来的栈内的矩形的面积
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <functional>
#include <utility>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <set>
#include <cmath>
#include <stdlib.h>
#include <climits>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment (linker, "/STACK:1024000000,1024000000")
typedef long long ll;
using namespace std;
const int maxn = 50100;
struct node
{
int w, h;
} sta[maxn];
int main()
{
int n;
while (~scanf("%d", &n) && n != - 1)
{
int ans = 0;
int top = 0;
for (int i = 1; i <= n; i++)
{
int w, h;
int tot = 0;
scanf("%d%d", &w, &h);
while (top > 0 && h < sta[top - 1].h)
{
tot += sta[top - 1].w;
if (ans < tot * sta[top - 1].h)
ans = tot * sta[top - 1].h;
top--;
}
sta[top].h = h;
sta[top++].w = w + tot;
}
int tot = 0;
while (top)
{
tot += sta[top - 1].w;
if (ans < tot * sta[top - 1].h)
ans = tot * sta[top - 1].h;
top--;
}
cout << ans << endl;
}
return 0;
}