POJ2559——DP——Largest Rectangle in a Histogram

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 3
4 1000 1000 1000 1000
0

Sample Output

8
4000

HINT

Huge input, scanf is recommended.

#include<cstdio>
using namespace std;
const int MAX = 1000000;
int h[MAX],L[MAX],R[MAX];
int stack[MAX];
int n;
void solve()
{
  int n;
  int t = 0;
    for(int i = 0 ; i < n; ++i){
        while(t > 0 && h[stack[t-1]] >= h[i]) t--;
        L[i] = ( t == 0) ? 0: stack[t-1];
        stack[t++] = i;
    }
    t = 0;
    for(int i = n - 1 ; i >= 0 ;--i){
        while(t > 0 && h[stack[t-1]] >= h[i]) t--;
        R[i] = (t == 0) ? n : stack[t-1];
       stack[t++] = i;
    }
    long long max1 = 0;
   for(int i = 0 ; i < n ;i++){
      max1 = max(max1,(long long)h[i]*(R[i] - L[i] + 1));
   }
  printf("%lld\n",max1);
}
int main()
{
    while(~scanf("%d",&n)&&n){
        for(int i = 0 ; i < n; i++)
            scanf("%d",&h[i]);
        solve();
    }
    return 0;
}

利用

L[i] = (j <= i并且h[j-1] < h[i]的最大的j)

R[i] = (j > i并且h[j] > h[i]的最小的j)

时间: 2024-12-15 01:56:35

POJ2559——DP——Largest Rectangle in a Histogram的相关文章

[dp]POJ2559 &amp;&amp; HDOJ1506 Largest Rectangle in a Histogram

题意 给n个条形的高度, 问能放的最大矩形面积 分析: 从左到右 从右到左 各搞一遍 分别记录      L[i]记录列(从前往后)标 第几列开始 可以往后放高度为a[i]的矩形  R[i]记录列(从后往前)标 第几列开始 可以往前放高度为a[i]的矩形 R[i]-L[i]+1即为高度为a[i]的矩形能横穿的列数 再乘个a[i]即为面积  遍历所有高度 求最大值即可 ps.注意范围 面积要用LL pps.注意 max((int), (LL)) 的CE 1 #include <cstdio> 2

HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1506 Appoint description: Description A histogram is a polygon composed of a sequence of rectangles aligned a

poj2559 Largest Rectangle in a Histogram

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 rectang

每日一dp(1)——Largest Rectangle in a Histogram(poj 2559)使用单调队列优化

Largest Rectangle in a Histogram 题目大意: 有数个宽为1,长不定的连续方格,求构成的矩形中最大面积 /************************************************************************/ /* 思路1. 当前为n的面积如何与n-1相联系,dp[i][j]=max(dp[i-1][k]) , 0<k<=j 描述:i为方块个数,j为高度 但是此题目的数据对于高度太变态,h,1000000000 ,n,1

POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831   Accepted: 5121 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

(单调栈)poj-2559 Largest Rectangle in a Histogram

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

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

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

POJ-2559 Largest Rectangle in a Histogram(单调栈)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22171   Accepted: 7173 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

POJ2559 Largest Rectangle in a Histogram (单调栈

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26012   Accepted: 8416 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi