您的位置:首页 > 其它

poj 2559 单调栈 ***

2015-07-23 16:45 302 查看
给出一系列的1*h的矩形,求矩形的最大面积。

如图:



题解链接:点我

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

const int N = 100005;

struct Elem
{
int height;
int count;
};

Elem stack
;
int top;

int main()
{
int height, n;
long long ans, tot, tmp;
while (scanf("%d", &n) != EOF && n)
{
top = 0;
ans = 0;
for (int i = 0; i < n; ++i)
{
scanf("%d", &height);
tmp = 0;
while (top > 0 && stack[top - 1].height >= height)
{
tot = stack[top - 1].height * (stack[top - 1].count + tmp);
if (tot > ans) ans = tot;
tmp += stack[top - 1].count;
--top;
}
stack[top].height = height;
stack[top].count = 1 + tmp;
++top;
}
tmp = 0;
while (top > 0)
{
tot = stack[top - 1].height * (stack[top - 1].count + tmp);
if (tot > ans) ans = tot;
tmp += stack[top - 1].count;
--top;
}
printf("%lld\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: