POJ2082 最大矩形面积

基本与POJ2559一样,只是初始矩形的宽度不再是1。

思路与POJ2559完全一致。详见http://blog.csdn.net/alongela/article/details/8230739。

#include <iostream>
#include <cstdio>
using namespace std;

const int N = 50005;

struct Elem
{
	int height;
	int width;
};

Elem stack[N];
int top;

int main()
{
	int height, width, n;
	int ans, tot, tmp;
	while (scanf("%d", &n) != EOF && n != -1)
	{
		top = 0;
		ans = 0;
		for (int i = 0; i < n; ++i)
		{
			scanf("%d%d", &width, &height);
			tmp = 0;
			while (top > 0 && stack[top - 1].height >= height)
			{
				tot = stack[top - 1].height * (stack[top - 1].width + tmp);
				if (tot > ans) ans = tot;
				tmp += stack[top - 1].width;
				--top;
			}
			stack[top].height = height;
			stack[top].width = width + tmp;
			++top;
		}
		tmp = 0;
		while (top > 0)
		{
			tot = stack[top - 1].height * (stack[top - 1].width + tmp);
			if (tot > ans) ans = tot;
			tmp += stack[top - 1].width;
			--top;
		}
		printf("%d\n", ans);
	}
	return 0;
}


你可能感兴趣的:(POJ2082 最大矩形面积)