HDU 1506 Largest Rectangle in a Histogram (线性dp)

针对大规模数据集,提出一种高效算法解决寻找直方图中最大子矩阵面积的问题。该算法通过预处理左右边界来减少不必要的计算,实现O(n)的时间复杂度。

Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 12865    Accepted Submission(s): 3621

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 3 4 1000 1000 1000 1000 0
 
Sample Output
  
8 4000
 
Source
 

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


题目大意:一排木板并排放,宽都是1,高是输入值,求所包含面积中的最大子矩形的面积


题目分析:由于本题的n比较大1e5,因此O(n^2)直接枚举的做法肯定T,我们考虑对其进行优化,对于每个木板,如果其左边或者右边的值大于等于它的值,则可以将它的左边界或右边界向左或向右延伸,这个很好理解,因为它的高度最小,假设为h1,则以其为中心扩展出的最大的矩形面积为h1*(区间长度)


#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 100005;
int h[MAX], l[MAX], r[MAX];

int main()
{
    int n;
    while(scanf("%d", &n) != EOF && n)
    {
        h[0] = h[n + 1] = -1; //这里初始化的值必须小于0,否则因为高度可能为0,左边界数组下标可能变为-1
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &h[i]);
            l[i] = r[i] = i;
        }
        for(int i = 1; i <= n; i++)
        {
            while(h[i] <= h[l[i] - 1])
            {
                l[i] = l[l[i] - 1];
                if(h[l[i]] == 0)
                    break;
            }
        }
        for(int i = n; i >= 1; i--)
        {
            while(h[i] <= h[r[i] + 1])
            {
                r[i] = r[r[i] + 1];
                if(h[r[i]] == 0)
                    break;
            }
        }
        ll ans = 0;
        for(int i = 1; i <= n; i++)
            ans = max(ans, (ll)h[i] * (ll)(r[i] - l[i] + 1));
        printf("%I64d\n", ans);
    }
}


dp的路好难走,越难越要走


源码来自:https://pan.quark.cn/s/41b9d28f0d6d 在信息技术领域中,jQuery作为一个广受欢迎的JavaScript框架,显著简化了诸多操作,包括对HTML文档的遍历、事件的管理、动画的设计以及Ajax通信等。 本篇文档将深入阐释如何运用jQuery达成一个图片自动播放的功能,这种效果常用于网站的轮播展示或幻灯片演示,有助于优化用户与页面的互动,使网页呈现更加动态的视觉体验。 为了有效实施这一功能,首先需掌握jQuery的核心操作。 通过$符号作为接口,jQuery能够迅速选取DOM组件,例如$("#id")用于选取具有特定ID的元素,而$(".class")则能选取所有应用了某类class的元素。 在选定元素之后,可以执行多种行为,诸如事件监听、样式的变更、内容的更新以及动画的制作等。 关于“一个基于jQuery的图片自动播放功能”,首要任务是准备一组图片素材,这些素材将被整合至一个容器元素之中。 例如,可以构建一个div元素,将其宽度设定为单张图片的尺寸,再借助CSS实现溢出内容的隐藏,从而构建出水平滚动的初始框架。 ```html<div id="slider"> <img src="image1.jpg" alt="Image 1"> <img src="image2.jpg" alt="Image 2"> <!-- 更多图片内容... --></div>```接着,需要编写jQuery脚本以实现图片的自动切换。 这通常涉及到定时器的运用,以设定周期性间隔自动更换当前显示的图片。 通过使用`.fadeOut()`和`.fadeIn()`方法,能够实现图片间的平滑过渡,增强视觉效果。 ```javascript$(document).re...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值