HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面積),hduhistogram

來源:互聯網
上載者:User

HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面積),hduhistogram

Problem DescriptionA 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:

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. 
InputThe 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. 
OutputFor 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 34 1000 1000 1000 10000
 
Sample Output
84000
 
SourceUniversity of Ulm Local Contest 2003 
Recommend


思路:以每一行的高度為高,看左右分別可以延伸到哪裡,算出面積求優解



代碼:



#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;#define N 100005__int64 h[N],le[N],ri[N];int main(){    __int64 n,i;    while(scanf("%I64d",&n),n)    {        for(i=1;i<=n;i++)        {            scanf("%I64d",&h[i]);            le[i]=ri[i]=i;        }        for(i=1;i<=n;i++)            while(le[i]>1&&h[le[i]-1]>=h[i])               le[i]=le[le[i]-1];        for(i=n;i>=1;i--)            while(ri[i]<n&&h[ri[i]+1]>=h[i])            ri[i]=ri[ri[i]+1];        __int64 ans=0;        for(i=1;i<=n;i++)        {            __int64 temp=((ri[i]-le[i]+1)*h[i]);            if(temp>ans)                ans=temp;        }        printf("%I64d\n",ans);    }    return 0;}






杭電1506,Largest Rectangle in a Histogram,幫忙看看

#include<stdio.h>__int64 h[100001],dp[100001],m1[100001],m2[100001];//數組太大了要放外面,其他的沒錯__int64 n,s,m,i,j,k;int main(){ while(scanf("%I64d",&n)!=EOF&&n!=0) { for(i=1;i<=n;i++) scanf("%I64d",&h[i]); m1[1]=1; for(i=1;i<=n;i++) { k=i; while(k>1) { if(h[k-1]>=h[i]) { m1[i]=m1[k-1]; k=m1[k-1]; } else { m1[i]=k; break; } } } m2[n]=n; for(i=n;i>=1;i--) { k=i; while(k<n) { if(h[k+1]>=h[i]) { m2[i]=m2[k+1]; k=m2[k+1]; } else { m2[i]=k; break; } } } for(i=1;i<=n;i++) dp[i]=(m2[i]-m1[i]+1)*h[i]; m=0; for(i=1;i<=n;i++) if(dp[i]>m) m=dp[i]; printf("%I64d\n",m); } return 0;}看了 很久剛剛開始我幫你驗證了一下,結果沒發執行,然後我就想可能是數組的原因吧,所以這問題以後注意一下就好了,,希望對你有協助

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.