面积最大 矩阵 01 java_HDU 1506 最大矩阵面积问题[DP]

Largest Rectangle in a HistogramTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7038    Accepted Submission(s): 1982

Problem Description

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:

eb5960f09a1245ebe970bb7af6446b63.gif

Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.

Input

The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1 <= n <= 100000. Then follow n integers h1, ..., hn, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.

Output

For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

Sample Input

7 2 1 4 5 1 3 3

4 1000 1000 1000 1000

0

Sample Output

8

4000

ca75c07623e1b494fee67e8f316fc310.gif

1fa987a29c6482f53d401256f96355eb.png1

1fa987a29c6482f53d401256f96355eb.png

ca75c07623e1b494fee67e8f316fc310.gif/**//*用dp方法解决,首先是对于每个h[i],都有s[i]=h[i]*(r-j+1),r为从i右边第一个起,若好h[r]>=h[i],则r++;左边同理28f1ba5b45633e9678d1db480c16cae3f.png最后求出max(s[i])即可*/34f1150b881333f12a311ae9ef34da474.png#include44f1150b881333f12a311ae9ef34da474.png#include54f1150b881333f12a311ae9ef34da474.pngusingnamespacestd;64f1150b881333f12a311ae9ef34da474.png#defineINF 10001074f1150b881333f12a311ae9ef34da474.pngintl[INF],r[INF],n,i;84f1150b881333f12a311ae9ef34da474.pngintmain()91fa987a29c6482f53d401256f96355eb.png

ca75c07623e1b494fee67e8f316fc310.gif9b8a8a44dd1c74ae49c20a7cd451974e.png{10d18c02628675d0a2c816449d98bda930.png    _int64 h[INF],s,max;//结果可能超出int,h初始为__int64 或运算前强转化为__int64。运算过程中int会溢出11d18c02628675d0a2c816449d98bda930.pngwhile(cin>>n&&n)1297e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gif9b8a8a44dd1c74ae49c20a7cd451974e.png{13d18c02628675d0a2c816449d98bda930.png        s=0;14d18c02628675d0a2c816449d98bda930.pngfor(i=1;i<=n;i++)1597e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gif9b8a8a44dd1c74ae49c20a7cd451974e.png{16d18c02628675d0a2c816449d98bda930.png            scanf("%I64d",&h[i]);17d18c02628675d0a2c816449d98bda930.png            l[i]=r[i]=i;18d18c02628675d0a2c816449d98bda930.png19ecedf933ec37d714bd4c2545da43add2.png        }20d18c02628675d0a2c816449d98bda930.png        h[0]=h[n+1]=-1;//题目中h[i]可能为0,初始化height数组为-1,防止死循环。21d18c02628675d0a2c816449d98bda930.pngfor(i=1;i<=n;i++)2297e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gif9b8a8a44dd1c74ae49c20a7cd451974e.png{23d18c02628675d0a2c816449d98bda930.pngwhile(h[l[i]-1]>=h[i])//左移24d18c02628675d0a2c816449d98bda930.pngl[i]=l[l[i]-1];25d18c02628675d0a2c816449d98bda930.png26ecedf933ec37d714bd4c2545da43add2.png        }27d18c02628675d0a2c816449d98bda930.pngfor(i=n;i>=1;i--)2897e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gif9b8a8a44dd1c74ae49c20a7cd451974e.png{29d18c02628675d0a2c816449d98bda930.pngwhile(h[r[i]+1]>=h[i])//右移30d18c02628675d0a2c816449d98bda930.png31d18c02628675d0a2c816449d98bda930.png                r[i]=r[r[i]+1];32ecedf933ec37d714bd4c2545da43add2.png        }33d18c02628675d0a2c816449d98bda930.png            max=0;34d18c02628675d0a2c816449d98bda930.pngfor(i=1;i<=n;i++)3597e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gif9b8a8a44dd1c74ae49c20a7cd451974e.png{36d18c02628675d0a2c816449d98bda930.png            s=h[i]*(r[i]-l[i]+1);37d18c02628675d0a2c816449d98bda930.png            max=s>max?s:max;38d18c02628675d0a2c816449d98bda930.png39ecedf933ec37d714bd4c2545da43add2.png        }40d18c02628675d0a2c816449d98bda930.png        printf("%I64d\n",max);41d18c02628675d0a2c816449d98bda930.png42ecedf933ec37d714bd4c2545da43add2.png    }43d18c02628675d0a2c816449d98bda930.pngreturn0;448f1ba5b45633e9678d1db480c16cae3f.png}

PS:有无有更加快速的方法呢,时间上还是比较慢

posted on 2013-05-01 23:19 天YU地___PS,代码人生 阅读(122) 评论(0)  编辑  收藏 所属分类: acm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值