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

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): 23837 Accepted Submission(s): 7462 Problem Description A... 阅读详情

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
 

Recommend


思路:以每一行的高度为高,看左右分别可以延伸到哪里,算出面积求优解



代码:



#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 100005

__int64 h[N],le[N],ri[N];

int main()
{
    __int64 n,i;

    while(scanf("%I64d",&n),n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%I64d",&h[i]);
            le[i]=ri[i]=i;
        }

        for(i=1;i<=n;i++)
            while(le[i]>1&&h[le[i]-1]>=h[i])
               le[i]=le[le[i]-1];

        for(i=n;i>=1;i--)
            while(ri[i]<n&&h[ri[i]+1]>=h[i])
            ri[i]=ri[ri[i]+1];

        __int64 ans=0;

        for(i=1;i<=n;i++)
        {
            __int64 temp=((ri[i]-le[i]+1)*h[i]);

            if(temp>ans)
                ans=temp;
        }

        printf("%I64d\n",ans);
    }
    return 0;
}





从零配置到稳定运行:A4988步进电机驱动模块的实战避坑指南 本文详细介绍了A4988步进电机驱动模块的配置与优化技巧,涵盖硬件连接、电流调节、微步设置及散热优化等关键环节。通过实战经验分享,帮助用户避开常见陷阱,实现步进电机的稳定运行,特别适合单片机开发者和3D打印机爱好者参考。 阅读详情

相关推荐

【Kingbase人大金仓】授权文件更换

本文介绍了金仓数据库授权文件更换的步骤:1.通过ksql或客户端工具查询数据库版本;2.在官网下载对应版本的license.bat文件;3.替换前先备份原授权文件(使用tar命令压缩),再用新文件覆盖原文件。注意事项包括:版本号需严格对应,授权文件通常位于安装目录下,可使用find命令查找具体位置。操作需谨慎,建议先备份原文件以便恢复。

qq_34463511的博客 3457

Largest Rectangle in a Histogram (连续矩形 够成矩形的最大面积 dp)

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

自渡的博客 467

高斯-约当(Gauss-Jordan)消元法——c++代码实现

具体算法的详细原理请参照此篇博客:高斯-约当(Gauss-Jordan)消元法下面是具体c++代码,不保证程序最优化,提供一个可行的程序,供大家改进,同时感谢大家提意见。——大神勿喷呀,我是一个小菜鸟~void Guss_Jordan(int n,double *a)//阶数n 系数矩阵a { double*a_temp = new double[n*n * 2];//a与单位矩阵的组合

zwz的学习之路 6872

[poj2559]Largest Rectangle in a Histogram(多种方法详细介绍)

Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33533 Accepted: 10940 Description A histogram is a polygon composed of a sequence of rectangles aligned at...

Skyed_blue的博客 731

win10任务栏文件夹设置成竖形列表样式

win10任务栏文件夹设置成竖形列表样式

ftzchina的博客 7078

保姆级教程:在RK3568 Debian系统上搞定AIC8800 WiFi与蓝牙驱动(含固件路径与开机自启脚本)

本文提供在RK3568 Debian系统上配置AIC8800 WiFi与蓝牙驱动的保姆级教程,涵盖驱动移植、固件部署、开机自启脚本等关键步骤。通过详细的硬件连接指南、内核配置示例和常见问题解决方案,帮助开发者快速实现无线模块的全功能配置,特别适合嵌入式Linux开发者参考。

weixin_28684825的博客 378

Largest Rectangle in a Histogram(dp)

咋一看,个人感觉十分简单,用个二重遍历,求出i之前的最小的矩形的高度以及相对应的连续的矩形的面积,再与自身的矩形比较,也就是找到矩形i左边与右边的比矩形i高的矩形的下标,这里分别用l[i]与r[i]来记录,通过计算公式。(r[i]-l[i]+1)*num[i]来实现对于每个i位置的最大的连续矩形的面积的求解。在寻求其他人的帮助后,发现根本不用两重遍历,通过两次一重遍历即可,大概的意思就是求连续的最大的矩形的面积。最终的话当然是accepted!但是提交之后会超时,

牛牛博士博客 303

汽车诊断服务(UDS——0x11服务解析)

ECU复位服务(0x11)详解:该服务支持硬件/钥匙开关/软件三种复位类型(0x01-0x03),复位前需发送肯定响应。请求格式为11+子功能,响应含51+子功能。典型应用需配合安全访问流程,测试时需监控CAN总线通信及DTC状态。复位后ECU将返回默认会话,期间不处理任何诊断请求。服务在编程会话且安全解锁后执行,硬复位会初始化存储数据。

从事汽车行业 979

Largest Rectangle in a Histogram

题意:让你找到最大的矩形。 ==>那么你就首先从左到右遍历,对于每一个高度向左考虑,找到第一个高度比它小的数,记录位置,向右考虑,找到一个高度比他小的数,记录位置。 然后再整体遍历一边,只需要比对高度*(边长)。 #include <iostream> #include <stdio.h> typedef long long ll; using namespace std; const int maxn = 1e6+10; const int INF = 0x3f3f3

weixin_60789461的博客 299

Windows非分页缓冲池内存泄漏实战:PoolMon与RAMMap高效排查指南

本文详细介绍了Windows系统中非分页缓冲池内存泄漏的实战排查方法。当系统出现卡顿且内存占用异常增长时,通过RAMMap工具快速定位非分页缓冲池异常,并利用PoolMon监控内核内存分配标签,精准锁定泄漏源。文章提供了从确认症状、安装工具到使用findstr命令溯源驱动文件的完整五步排查流程,并总结了常见泄漏标签与解决方案,帮助用户高效解决深层系统内存问题。

weixin_28455801的博客 1008

基于JeecgBoot 3.9.0 开源项目的企业管理平台

基于JeecgBoot 3.9.0 开源项目的企业管理平台

宁波阿成的博客 371

LeetCode 84.柱状图中的最大矩形/POJ 2559

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

缇欧的咪西 520

Windows 下 Claude Code Desktop 安装与使用全记录:从零到一的保姆级图文教程

本文主要介绍了Windows 下 Claude Code 安装全记录:从零到一的保姆级图文教程,希望能对学习使用 Claude Code 的同学们有所帮助。 文章目录 1. 背景 2. 安装Claude Code Desktop 3. 配置第三方API 4. 日常使用

weixin_43178406的博客 8万+

[POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

[POJ 2559]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 ...

aua4137的博客 186

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

最大完全子块

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 th...

你倒是敲代码鸭. 583

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Largest Rectangle in a Histogram (动态规划+奇思妙想单调栈)求最大矩状图面积

感觉动态规划都是玄妙的很,思维题吧(单调栈思维) 题解:让求最大矩形面积,宽为1,暴力超时 可以发现 当第i-1个比第i个高的时候 比第i-1个高的所有也一定比第i个高 于是可以用到动态规划的思想 令left[i]表示包括i在内比i高的连续序列中最左边一个的编号 right[i]为最右边一个的编号 那么有 当h[left[i]-1]>=h[i]]时 le...

zeng_jun_yv的博客 430

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

HDU1506 Largest Rectangle in a Histogram 直方图中最大的矩形(单调栈/动态规划/笛卡尔树)

Largest Rectangle in a Histogram

xinchu0309的博客 214

重要基础数据结构——单调栈

例题 例题1 SP1805 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...

atxe89425的博客 528

POJ ——1056 —— Largest Rectangle in a Histogram

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 figu...

weixin_43751631的博客 261

Largest Rectangle in a Histogram(单调栈)

Largest Rectangle in a Histogram Time Limit:1000MS Memory Limit:65536K Total Submissions:33874 Accepted:11045 Description A histogram is a polygon composed of a sequence of r...

点点滴滴 232

【HDU.2559】Largest Rectangle in a Histogram(单调栈)

Largest Rectangle in a Histogram 直方图中最大的矩形 Language: Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36046 Accepted: 11781 Description A histogram is a polygon composed of a sequence of rectangles aligned at a

我很菜的好吧 1773

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): 17827    Accepted Submission(s): 5330 Problem Descr

香橙的博客 428

Largest Rectangle in a Histogram(单调栈)

Sample Input 7 2 1 4 5 1 3 3 4 1000 1000 1000 1000 0 Sample Output 8 4000 求题意就是让求最大的矩形面积。 思路:这里我们既然相求最大的矩形面积,所以我们就判断以单个单个矩形的高为起点,想左右拓展的最元距离。最后维护一个最大值。但是我们用暴力判断能向左右拓展多远明显时间复杂度又不够,所以我们采用单调栈的思想,比如我们先从左到右判断以没个点为高的左边界时,我们就从左到右维护一个递增的栈。每次判断下标所在的值,与栈顶所在的值得关系.

s1050712899的博客 833

Largest Rectangle in a Histogram HDU - 1506 解题思路 单调栈

原题目 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 r

qq_45750017的博客 976
上一篇: POJ 1094 Sorting It All Out(topsort Language: Sorting It All Out Time Limit: 1000MS Memory Limit: )
下一篇: HDU 2845 Beans(dp)
左眼皮跳跳~
博客等级 码龄13年 17粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值