hdu 1506

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.

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

// 第一个数 n ,代表后面有几个数,后面 n 个数代表长方体的高,宽都是 1 ,并且n个长方体紧靠,问最大的矩形面积

L[i] 代表 h[j]>=h[i](j<=i) 的最远的编号

R[i] 代表 h[j]>=h[i](j>=i) 的最远的编号

求出来后,就可以去找最大矩形了,遍历一遍 maxS = max( (R[i]-L[i]+1)*h[i] ) (1<=i<=n)

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 100005
#define LL long long
LL L[MAXN];
LL R[MAXN];
LL h[MAXN]; int main()
{
int n;
while (scanf("%d",&n)&&n)
{
for (int i=;i<=n;i++)
scanf("%I64d",&h[i]);
h[]=h[n+]=-;
for (int i=;i<=n;i++)
{
L[i]=i;
while (h[L[i]-]>=h[i]) //利用 L[] 去循环,其实很快
L[i]=L[L[i]-];
}
for (int i=n;i>=;i--)
{
R[i]=i;
while (h[R[i]+]>=h[i])
R[i]=R[R[i]+];
}
LL ans = ;
for (int i=;i<=n;i++)
{
LL area = (R[i]-L[i]+)*h[i];
if(area>ans) ans = area;
}
printf("%I64d\n",ans);
}
return ;
}

Largest Rectangle in a Histogram (最大子矩阵)的更多相关文章

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

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 刚开始没考虑时间复杂度,直接敲了,直接tle了,之后没有思路,然后看题解,看见大神写的优化非常棒. ...

  2. hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」

    Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...

  4. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  5. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  6. Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K ...

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

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  8. Largest Rectangle in a Histogram(HDU1506)

    Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...

  9. POJ 2559 Largest Rectangle in a Histogram

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18942   Accepted: 6083 Description A hi ...

随机推荐

  1. 通过脚本发送zabbix微信报警

    实现zabbix通过微信报警的方式也是通过脚本来实现,与邮件报警不同的是,脚本调用的微信的相关接口的获取相对复杂一点 1.申请一个微信公众号(企业号) 申请方法不多说,如果已申请请忽略 2.在微信企业 ...

  2. 基于ruby环境搭建Redmine

    环境说明 系统版本    CentOS 6.9 x86_64 软件版本    ruby 2.4.4 rails 4.2 redmine-3.4.5 Redmine是一个开源的.基于Web的项目管理和缺 ...

  3. Binder与interface

    在Interface中,asBinder函数涌来将服务类接口类型转换为IBinder类型: 相反的,asInterface函数用来将Ibinder类型转换为服务接口类型

  4. Fragment生命周期和Activity生命周期的关系

    Fragment生命周期: 重点回调函数: 1. onAttach()当碎片和活动建立关联的时候调用.2. onCreateView()为碎片创建视图(加载布局)时调用.3. onActivityCr ...

  5. SQL语句练习手册--第一篇

    表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师 ...

  6. sqlserver 字段内容做in条件 列变成行显示

    sqlserver中 字段内容做in条件用到方法:CHARINDEX(value,situation) 列变行显示用到:stuff 详情自行查找. 例子: stuff((select ','+name ...

  7. 基于SpringMVC+Ext.js的权限管理系统(无权限框架)

    代码地址如下:http://www.demodashi.com/demo/12811.html 0.准备工作 注意!!! 本案例数据库相关请下载例子包,内有数据库脚本.EXCEL数据表和详细的设计文档 ...

  8. AVCaptureSession

    AVCaptureSession用来控制来自一个输入设备(AVCaptureDeviceInput)的声音和视频,流入一个输出缓冲区(AVCaptureOutput)的过程. 建立一个AVCaptur ...

  9. Hive优化策略

    hive优化目标 在有限的资源下,运行效率高. 常见问题 数据倾斜.Map数设置.Reduce数设置等 hive运行 查看运行计划 explain [extended] hql 例子 explain ...

  10. jvm性能调优常用命令

    说明和名词解释: ①  只有进行的运行用户才可以调用命令查看相关信息 ②  [pid] 为需要查看的进程的端口号 ③  [file] 为需要导出到的文件的具体地址 ④ [tid] 进程中线程的id 1 ...