最大子矩形POJ2559

最大子矩形POJ2559

思路

题干在这:POJ2559
简简单单的单调栈

ac代码

#include
#include
#include
#define ll long long
using namespace std;
struct rectangle {
	ll height;
	ll length;
};
vector<rectangle> squ;
int main() {
	while (1) {
		ll n;
		scanf_s("%lld", &n);
		if (n == 0)
			return 0;
		ll maxs = 0;
		for (ll i = 0; i <= n; i++) {
			ll h = 0;
			if (i != n)
				scanf_s("%lld", &h);
			if (squ.empty() || h >= squ.back().height) {
				rectangle q = { h,1 };
				squ.push_back(q);
			}
			else {
				ll l = 0;
				while (!squ.empty() && h < squ.back().height) {
					l += squ.back().length;
					maxs = max(maxs, l * squ.back().height);
					squ.pop_back();
				}
				if (i != n) {
					rectangle q = { h,l + 1 };
					squ.push_back(q);
				}
			}
		}
		printf("%lld\n", maxs);
	}
}

你可能感兴趣的:(算法竞赛进阶指南练习)