Algorithm学习笔记 --- Largest Rectangle in a Histogram

来源:互联网 发布:adidas淘宝官方旗舰店 编辑:程序博客网 时间:2024/06/05 22:54

此题也是一道典型的单调队列例题,此题也可以用dp,搜索做,搜索和dp会简单一些。

Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13965 Accepted: 4498

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 integersh1,...,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

Hint

Huge input, scanf is recommended.

Source

Ulm Local 2003



解题分析:

此题提议大概是求解矩形中面积最大的矩形面积。

输入为:

首先是输入N个用例,然后依次输入每组数据,对应的输出N个用例对应的最大面积。


做法:这里用的是单调队列求解。

做法是设两个数组一个存储原数组的元素,另一个是存储一个(i,j)这个范围内的数,

i-1一定要比i小,j+1一定也要小于等于j。可能有多个范围,要求出最大的范围,然后用范围j-i的值,就是长度,然后乘以高度。求出面积。


代码:

#include<stdio.h>
#include<algorithm>
#define MAX_N 100009
using namespace std;
int rect[MAX_N];
struct seg
{
    long long  value,s;
}q[MAX_N];
int main()
{
    int n ;
    while(scanf("%d",&n)&&n)
    {
        long long area = 0 ,top = 0;
        for(int i=0;i!=n;++i)
            scanf("%d",rect+i);
        for(int i=0;i!=n;++i)
        {
            //队列为空或者当前元素大于队尾时入队
            if(!top || q[top-1].value < rect[i])
            {
                q[top].value = rect[i];
                q[top++].s = i ;
            }
            //此时,右边界出现,需要做更新
            else if(q[top-1].value>rect[i])
            {
                //如果有比最大值大的面积,则更新最大值
                while(top && q[top-1].value>rect[i])
                {
                    --top;
                    area = max(area,q[top].value*(i-q[top].s));
                }
                //队空
                if(!top)
                    q[top++].value = rect[i] ;
                else if(q[top-1].value < rect[i])
                    q[top++].value = rect[i] ;
            }
        }
        //队中可能还有元素,也要做相应的更新
        while(top)
        {
            --top;
            area = max(area,q[top].value*(n-q[top].s));
        }
        printf("%lld\n",area);
    }
    return 0;
}

0 0
原创粉丝点击