POJ 2559 Largest Rectangle in a Histogram(单调栈)

来源:互联网 发布:数据库垂直切分 编辑:程序博客网 时间:2024/05/22 21:22

题目链接:POJ 2559 Largest Rectangle in a Histogram

#include <iostream>#include <cstdio>#include <stack>using namespace std;struct Node{    int l;    long long h;};int main(){    int n;    while(scanf("%d", &n), n)    {        long long h, k = 0, _max = 0;        stack <Node> S;        Node temp, tmp;        for(int i = 0; i < n; i++)        {            scanf("%lld", &h);            temp.l = i, temp.h = h;            while(!S.empty() && S.top().h >= h)            {                tmp = S.top();                S.pop();                k = tmp.h * (i - tmp.l);                temp.l = tmp.l;                _max = max(_max, k);            }            S.push(temp);        }        while(!S.empty())        {            tmp = S.top();            S.pop();            k = tmp.h * (n - tmp.l);            _max = max(_max, k);        }        printf("%lld\n", _max);    }    return 0;}


0 0