zoj 1985 - Largest Rectangle in a Histogram

题目:给你一些不同高度的宽度为1的木板,问能截取最大矩形面积。

分析:dp,单调队列。关键在于找到每个高度的最大连续长度,最大面积了 O(N*max(R - L));

如果暴力的话,则代价为O(N),则总代价为O(N*N)无法处理100000数据量;

但是可用单调队列,做预处理 用O(N)时间计算出所有点的边界,此时时间复杂度为 O(N);

每个元素从单调队列中出去的时间就是找到第一个不符合条件的点的时候,两边用0为边界值。

说明:很多人N^2过了,数据可能有点弱。(2011-09-19 01:37)

#include <stdio.h>
#include <stdlib.h> 

long long stick[ 100005 ];
int     MUQ[ 100005 ];
int  L[ 100005 ],R[ 100005 ];

int main()
{
    int n;
    while ( scanf("%d",&n) && n ) {
        for ( int i = 1 ; i <= n ; ++ i )
            scanf("%lld",&stick[ i ]);
        stick[ 0 ] = -1; stick[ n+1 ] = -1;

        /* 单调队列预处理,求出每个stick的右边的第一个小于他高度的stick位置 */
        int tail = 0;
        MUQ[ 0 ] = 0;
        for ( int i = 1 ; i <= n+1 ; ++ i ) {
            while ( tail >= 0 && stick[ MUQ[ tail ] ] > stick[ i ] )
                R[ MUQ[ tail -- ] ] = i;
            MUQ[ ++ tail ] = i;
        }

        /* 单调队列预处理,求出每个stick的左边的第一个小于他高度的stick位置 */
        tail = 0;
        MUQ[ 0 ] = n+1;
        for ( int i = n ; i >= 0 ; -- i ) {
            while ( tail >= 0 && stick[ MUQ[ tail ] ] > stick[ i ] )
                L[ MUQ[ tail -- ] ] = i;
            MUQ[ ++ tail ] = i;
        }

        long long Max = 0,Temp = 0;
        for ( int i = 1 ; i <= n ; ++ i ) {
            Temp = stick[ i ]*(R[ i ]-L[ i ]-1);
            if ( Max < Temp )
                Max = Temp;
        }
        printf("%lld\n",Max);
    }
    return 0;
}
时间: 2024-10-13 00:20:28

zoj 1985 - Largest Rectangle in a Histogram的相关文章

(单调栈)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

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

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

[题目链接]:click here~~ [题目大意]: 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 con

hdu 1506 Largest Rectangle in a Histogram 单调队列

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12554    Accepted Submission(s): 3509 Problem Description A histogram is a polygon composed of a sequence of rec

HDU 1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 150664-bit integer IO format: %I64d      Java class name: Main A histogram is a polygon composed of a sequence of rectangles al

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

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

HDU1506 Largest Rectangle in a Histogram (动规)

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10873    Accepted Submission(s): 2969 Problem Description A histogram is a polygon composed of a sequence of rec