Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11541    Accepted Submission(s): 3174

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
 
题意: 给你一个条形图,数值,如上图,要你求出其最大矩形.....
就是给定了你的h(高),要你求宽度....宽度这个是等于j-i+1这样一个长度:
 对于任意一个位置,我们只需要找到他可以组合的最左边,和最右边就可以求出宽度...
于是,(这个思想是参考discuss里别人的....╮(╯▽╰)╭,然后自己理解的)我们不妨做两次求值:
首先从1~~N ,遍历,对于pos这个位置,求出左边比他高的数的下标.....
然后,从N~~!遍历,对于pos这个位置,求出右边比他高的数的下标.....
比如:
  7  2 1 4 5 1 3 3  这组数:
 求解步骤:
  pos   1 2 3 4 5 6 7
  val   2 1 4 5 1 3 3
 ll[]   1 1 3 4 1 6 6  1->n
 rr[]   1 7 4 4 7 7 7  n->1
 
res=(rr[pos]-ll[pos])*hh[pos];
代码:
 #include<cstdio>
#include<cstring>
#define maxn 100005
__int64 ll[maxn],rr[maxn],hh[maxn];
int main()
{
int n,i,t;
while(scanf("%d",&n)&&n)
{
for(i=;i<=n;i++)
scanf("%I64d",&hh[i]);
ll[]=,rr[n]=n;
for(i=;i<=n;i++)
{
t=i;
while(t>&&hh[i]<=hh[t-])
t=ll[t-];
ll[i]=t;
}
for(i=n-;i>;i--)
{
t=i;
while(t<n&&hh[i]<=hh[t+])
t=rr[t+];
rr[i]=t;
}
__int64 res=;
for(i=;i<=n;i++)
{
if(res<(rr[i]-ll[i]+)*hh[i])
res=(rr[i]-ll[i]+)*hh[i];
}
printf("%I64d\n",res);
}
return ;
}
  
     

hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)的更多相关文章

  1. HDU 1506 Largest Rectangle in a Histogram(DP)

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

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

    # include <stdio.h> # include <algorithm> # include <iostream> # include <math. ...

  3. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  4. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  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. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  7. hdu 1506 Largest Rectangle in a Histogram 构造

    题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  8. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

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

  9. hdu 1506 Largest Rectangle in a Histogram(单调栈)

                                                                                                       L ...

随机推荐

  1. V-rep学习笔记:曲柄摇杆机构

    在ADAMS中创建一个曲柄摇杆机构很方便,但是V-rep中建模就比较麻烦.下面将自己在V-rep中建立曲柄摇杆机构模型的过程记录下来(由于对V-rep不是很熟,可能会有一些错误,只能等以后发现了再改进 ...

  2. MongoDB开发学习

    如果你从来没有接触MongoDB或对MongoDB有一点了解,如果你是C#开发人员,那么你不妨花几分钟看看本文.本文将一步一步带您轻松入门. 阅读目录 一:简介 二:特点 三:下载安装和开启服务器 四 ...

  3. activiti5.15中文乱码问题

    解决方式: 1.配置文件插入 <bean id="processEngineConfiguration" class="org.activiti.spring.Sp ...

  4. SAP接口编程 之 JCo3.0系列(04) : 会话管理

    在SAP接口编程之 NCo3.0系列(06) : 会话管理 这篇文章中,对会话管理的相关知识点已经说得很详细了,请参考.现在用JCo3.0来实现. 1. JCoContext 如果SAP中多个函数需要 ...

  5. iOS常用define宏定义

    1. 屏幕宽高及常用尺寸 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)#define SCREEN_HEIGHT ([U ...

  6. python操作postgresql数据库

    import psycopg2 conn = psycopg2.connect(database=") cur = conn.cursor() cur.execute("CREAT ...

  7. Oracle 修改一行数据内存主要变化

    向Oracle 数据库发出请求,修改一行数据,在内存中主要有以下变化: 1. 服务器进程将包含该行数据的块读取到内存中 2. 写redo日志.将内存中该数据块指向undo表空间中数据块的变更向量(Ch ...

  8. 转 java List 与ArrasyList 区别

    转 List是一个接口,而ListArray是一个类, ListArray继承并实现了List. 为什么要用 List list = new ArrayList() ,而不用 ArrayList al ...

  9. [转载]java中try 与catch的使用

    留着以后看 原文地址:与catch的使用">java中try 与catch的使用作者:碌碌如玉 try{ //代码区 }catch(Exception e){ //异常处理 } 代码区 ...

  10. switch-case 执行顺序

    switch(expression){ case value : //语句 break; //可选 case value : //语句 break; //可选 //你可以有任意数量的case语句 de ...