HDU1506: Largest Rectangle in a Histogram(最大子矩阵,好题动态优化左右边界)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506

刚开始没考虑时间复杂度,直接敲了,直接tle了,之后没有思路,然后看题解,看见大神写的优化非常棒。

大神的解释:(其实对于w[i]来说,如果去求后面连续的值,完全没必要一个个去比对,直接看w[i-1]的值就行了。

比如说2、3、4、5这个序列,如果我们要看3往后能延伸多长,并不需要去逐个和4和5比较,在计算4的时候,我们已经计算过5是比4大的,因为3比4小,4所能往后延伸的长度,3也一定能达到(4能延伸的长度内的数据都大于等于4,当然也都比3大),我们可以直接比较在4达到的最终长度的末端之后的值。(dp进行记录)

这道题计算的时候进制转换也需要特别注意,如果temp没有强制转换成__int64位,提交会wa。注意强制转换啊,否则wa.)

这道题优化的思路非常巧妙,很值得学习。

DP 找出 a[i] 的左边(l[i])和右边(r[i])与自己连着的比自己大的数的长度 , 然后用这个长度乘以 a[i], 乘积最大的那个就是答案 .

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define inf 0x3f3f3f3f
typedef __int64 ll;
using namespace std;
int n,w[100010],l[100010],r[100010];
int main()
{
    ll maxx,zan;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        for(int i=1;i<=n;i++)
        {
          scanf("%d",&w[i]);
          l[i]=r[i]=i;
        }
        for(int i=2;i<=n;i++)
        {
            while(l[i]>1&&w[l[i]-1]>=w[i])
            {
                l[i]=l[l[i]-1];
            }
        }
        for(int i=n-1;i>=1;i--)
        {
            r[i]=i;
            while(r[i]<n&&w[r[i]+1]>=w[i])
            {
                r[i]=r[r[i]+1];
            }
        }
        maxx=-inf;
        for(int i=1;i<=n;i++)
        {
            zan=(ll)(r[i]-l[i]+1)*w[i];//以前一直对变量的存储有个错误的了解,如果(r[i]-l[i]+1)*w[i]
            maxx=max(maxx,zan);//如果不强制转换的话,则会wa,以后做题注意
        }
       printf("%I64d\n",maxx);
    }
    return 0;
}

tle的水代码。

include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define inf 0x3f3f3f3f
typedef __int64 ll;
using namespace std;
int n,w[100010];
ll dp[100010];
int main()
{
    int minx;
    ll maxx,zan;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&w[i]);
        memset(dp,0,sizeof(dp));
        dp[1]=w[1];
        maxx=w[1];
        for(int i=2;i<=n;i++)
        {
            minx=w[i];
            for(int j=i-1;j>=1;j--)
            {
                minx=min(minx,w[j]);
                zan=(i-j+1)*minx;
                dp[i]=max(dp[i],zan);
            }
            maxx=max(maxx,dp[i]);
        }
        printf("%I64d\n",maxx);
    }
    return 0;

时间: 2024-10-01 05:00:08

HDU1506: Largest Rectangle in a Histogram(最大子矩阵,好题动态优化左右边界)的相关文章

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

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): 12019    Accepted Submission(s): 3326 Problem Description A histogram is a polygon composed of a sequence of rec

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): 26267    Accepted Submission(s): 8279 Problem Description A histogram is a polygon composed of a sequence of rect

【单调栈】hdu1506 Largest Rectangle in a Histogram

单调栈的介绍及一些基本性质 http://blog.csdn.net/liujian20150808/article/details/50752861 依次把矩形塞进单调栈,保持其单增,矩形中的元素是一个三元组,存储其位置,高度,以及以其为高度的情况下,大矩形的左边界最多扩展到哪里. 每次将新的元素塞进栈的时候,其左边界就是其左侧第一个小于它的矩形的位置+1. 然后,每个矩形出栈的时候,记录其右边界为当前往栈里面塞的矩形的位置-1,然后更新答案即可. 注意最后把所有的矩形出栈,更新答案. #in

[hdu1506 Largest Rectangle in a Histogram]笛卡尔树

题意:http://acm.hdu.edu.cn/showproblem.php?pid=1506 如图,求最大的矩形面积 思路: 笛卡尔树:笛卡尔树是一棵二叉树,树的每个节点有两个值,一个为key,一个为value.光看key的话,笛卡尔树是一棵二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:光看value的话,笛卡尔树有点类似堆,根节点的value是最小(或者最大)的,每个节点的value都比它的子树要小(或者大). 笛卡尔树的构造算法:从右链插入,同时维护右链的递增或递减序列

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