HDU 1506 Largest Rectangle in a Histogram

来源:互联网 发布:中国联通淘宝旗舰店 编辑:程序博客网 时间:2024/06/05 11:40


http://acm.hdu.edu.cn/showproblem.php?pid=1506

s[i] = ((r[i]-1)-(l[i]+1) + 1) * h[i]

r[i]代表i的右边第一个高度小于i的矩形的标号,l[i]代表i的左边第一个高度小于i的矩形的标号。

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;const int N = 100010;LL h[N],l[N],r[N];int n;int main(){//    freopen("in.txt", "r", stdin);    while(scanf("%d",&n) != EOF && n){        for(int i=1; i<=n; i++){            scanf("%I64d",&h[i]);            l[i] = i - 1, r[i] = i + 1;        }        h[0] = h[n+1] = -1;        for(int i=1; i<=n; i++)            while(h[l[i]] >= h[i])                l[i] = l[l[i]];        for(int i=n; i>=1; i--)            while(h[r[i]] >= h[i])                r[i] = r[r[i]];        LL ans = -1ll;        for(int i=1; i<=n; i++)            ans = max(ans, ((r[i]-1)-(l[i]+1) + 1) * h[i]);        printf("%I64d\n",ans);    }    return 0;}

0 0