hdu 1506 Largest Rectangle in a Histogram(dp)

来源:互联网 发布:java scanner语句用法 编辑:程序博客网 时间:2024/05/20 11:49

Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13467    Accepted Submission(s): 3810


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:

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 34 1000 1000 1000 10000
 

Sample Output
84000
 



连续长方形的最大面积

对于每个位置 记录左边连续比它大的最左边的下标  记录右边连续比它大的最右边的下标

然后可以做这题的加强版 hdu 1505

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 100010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char c = getchar();    while (c < '0' || c > '9') c = getchar();    int x = 0;    while (c >= '0' && c <= '9') {        x = x * 10 + c - '0';        c = getchar();    }    return x;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}ll a[MAXN],l[MAXN],r[MAXN];ll Max(ll a,ll b){    if(a>b)  return a;    return b;}int main(){//    fread;    ll n;    while(scanf("%I64d",&n),n)    {        for(ll i=1;i<=n;i++)            scanf("%I64d",&a[i]);        l[1]=1; r[n]=n;        ll t;        for(ll i=2;i<=n;i++) //求每个点左边连续比它大的最左边的下标,保存在l[]数组里        {            t=i;            while(t>1&&a[i]<=a[t-1])                t=l[t-1];            l[i]=t;        }        for(ll i=n-1;i>=1;i--) //求每个点右边连续比它大的最右边的下标,保存在r[]数组里        {            t=i;            while(t<n&&a[i]<=a[t+1])                t=r[t+1];            r[i]=t;        }        ll mx=0;        for(ll i=1;i<=n;i++)        {            ll tmp=(r[i]-l[i]+1)*a[i];            mx=Max(mx,tmp);        }        printf("%I64d\n",mx);    }    return 0;}





0 0
原创粉丝点击