HDU1506------据说是DP,我更觉得是简单的枚举

来源:互联网 发布:mysql 5.6.25.tar.gz 编辑:程序博客网 时间:2024/05/10 12:24

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1506

题目意思:

给你n,然后告诉你这n段的高度,这样就组成了一个直方图

为你直方图中最大的矩形的面积是多少

解题思路:

枚举啦

对于每个高度h[i],都找出他能到的最左边以及最右边

然后再求面积

但是在找最左边和最右边的时候是有技巧的

对于h[i]而言,如果左边的h[i-1]比他大,则他的最左边一定是i-1的最左边,然后一直迭代下去

具体详见代码:

#include<cstdio>#include<algorithm>using namespace std;#define LL long longconst int maxn = 100000+10;LL l[maxn];LL r[maxn];LL h[maxn];int main(){    int n;    while(~scanf("%d",&n) && n)    {        for(int i=1;i<=n;i++)            scanf("%I64d",&h[i]);        //通过迭代在找对于每个h[i],最左边能到哪        l[1]=1;        //确定左右高度的边界值,很重要        h[0]=-1;        h[n+1]=-1;        for(int i=2;i<=n;i++)        {            l[i]=i;            while(h[l[i]-1]>=h[i])            {                l[i]=l[l[i]-1];            }        }        r[n]=n;        for(int i=n-1;i>=1;i--)        {            r[i]=i;            while(h[i]<=h[r[i]+1])                r[i]=r[r[i]+1];        }        LL ans = -1;        LL tmp;        for(int i=1;i<=n;i++)        {            tmp = (r[i]-l[i]+1)*h[i];            if(tmp>ans)                ans=tmp;        }        printf("%I64d\n",ans);    }    return 0;}


原创粉丝点击