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. 5分钟教程:如何通过UART获得root权限

    写在前面的话 你知道物联网设备以及其他硬件制造商是如何调试和测试自家设备的吗?没错,绝大多数情况下,他们都会留下一个串行接口,这样就可以利用这个接口并通过shell来读取实时的调试日志或与硬件进行交互 ...

  2. JAVA之继承的必要性

    //说明继承的必要性package com.test; public class test { /**     * @param args     */    public static void m ...

  3. Litepal 数据库操作框架的使用 (火)

    LitePal是GitHub上一款开源的Android数据库框架. 它採用了对象关系映射(ORM)的模式,将平时开发时最经常使用的一些数据库功能进行了封装.使得开发人员不用编写一行SQL语句就能够完毕 ...

  4. 修改kafka broker.id

    kafka.common.InconsistentBrokerIdException: Configured broker.id 1 doesn't match stored broker.id 0 ...

  5. 【Hadoop】Hadoop MR 自定义排序

    1.概念 2.代码示例 FlowSort package com.ares.hadoop.mr.flowsort; import java.io.IOException; import org.apa ...

  6. input 中 datetime-local 方法

    <input  type=" datetime-local "> 这个标签是H5新增的对象方法 能把现有的时间赋值给它 但是注意:必须是  yyyy-MM-ddTHH: ...

  7. 2017.7.1 nginx反向代理服务器域名解析配置(已验证可使用)

    下载地址:http://learning.happymmall.com/ 前提:ftpserver已经开启,并且设置为: 1.获得安装文件 2.修改配置文件 2.1 修改conf/nginx.conf ...

  8. PHP使用mysqli连接MySQL数据库

    使用mysqli函数库连接MySQL,支持面向对象和面向过程两种方式: 1.面向对象的使用方式 建立一个连接 $db = new mysqli('localhost', 'root', '123456 ...

  9. 【BIEE】02_新建资料库并创建简单分析

    一.新建资料库 1.开始→打开BI管理→点击新建资料库 2.文件→新建资料档案库 下一步 在下面的框中一次填入 连接类型:OCI 10g/11g(直接选择即可) 数据库名称:(DESCRIPTION ...

  10. swift 可变參数

    func sumof(numbers:Int...)->Int{  var sum = 0;  for number in numbers{   sum+=number;  }  return ...