HDU 1506 Largest Rectangle in a Histogram(DP),hduhistogram

來源:互聯網
上載者:User

HDU 1506 Largest Rectangle in a Histogram(DP),hduhistogram

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11137    Accepted Submission(s): 3047


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
 

題意  求橫條圖中最大矩形的面積  輸入給你條的個數  每個條的高度hi   (以下等於也視為高)

只要知道第i個條左邊連續多少個(a)比他高   右邊連續多少個(b)比他高  那麼以這個條為最大高度的面積就是hi*(a+b+1);

但是直接枚舉每一個的話肯定會逾時的   逾時代碼

#include<cstdio>using namespace std;const int N = 100005;typedef long long ll;ll h[N]; int n,wide[N];int main(){    while (scanf ("%d", &n), n)    {        for (int i = 1; i <= n; ++i)            scanf ("%I64d", &h[i]);        for (int i = 1; i <= n; ++i)        {            wide[i] = 1;            int k = i;            while (k > 1 && h[--k] >= h[i]) ++wide[i];            k = i;            while (k < n && h[++k] >= h[i]) ++wide[i];        }        ll ans = 0;        for (int i = 1; i <= n; ++i)            if (h[i]*wide[i] > ans) ans = h[i] * wide[i];        printf ("%I64d\n", ans);    }    return 0;}

可以發現   當第i-1個比第i個高的時候   比第i-1個高的所有也一定比第i個高  

於是可以用到動態規劃的思想

令left[i]表示包括i在內比i高的連續序列中最左邊一個的編號  right[i]為最右邊一個的編號

那麼有   當h[left[i]-1]>=h[i]]時   left[i]=left[left[i]-1]  從前往後可以遞推出left[i]   

同理      當h[right[i]+1]>=h[i]]時   right[i]=right[right[i]+1]   從後往前可遞推出righ[i]

最後答案就等於 max((right[i]-left[i]+1)*h[i])了

#include<cstdio>using namespace std;const int N = 100005;typedef long long ll;ll h[N];int n, left[N], right[N];int main(){    while (scanf ("%d", &n), n)    {        for (int i = 1; i <= n; ++i)            scanf ("%I64d", &h[i]), left[i] = right[i] = i;        h[0] = h[n + 1] = -1;        for (int i = 1; i <= n; ++i)            while (h[left[i] - 1] >= h[i])                left[i] = left[left[i] - 1];        for (int i = n; i >= 1; --i)            while (h[right[i] + 1] >= h[i])                right[i] = right[right[i] + 1];        ll ans = 0;        for (int i = 1; i <= n; ++i)            if (h[i] * (right[i] - left[i] + 1) > ans) ans = h[i] * ll (right[i] - left[i] + 1);        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.