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. R: NULL, NA, and NaN

    NaN (“Not a Number”) means 0/0 NA (“Not Available”) is generally interpreted as a missing value and ...

  2. [CF738D]Sea Battle(贪心)

    题目链接:http://codeforces.com/contest/738/problem/D 题意:1*n的格子里有a条长为b的船.有一个人射了k发子弹都没打中船,现在问最少再打多少次一定能保证射 ...

  3. git学习笔记12-标签管理-版本

    发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一个快照 ...

  4. tiled工具使用

    转的 在这个分为上下两部分的教程中,我们将介绍如何使用Cocos2D-X和地图编辑器做一款基于地图块的游戏.在这个简单的地图块游戏里,一个精灵将在沙漠里搜寻它可口的西瓜! 在教程的第一部分,我们将介绍 ...

  5. shell script的连接符是逗号,不是英文的句号

    举个例子: gawk 'BEGIN{ var[ var[ var[ var[ asort(var,test) for(i in test) print ] }' 这时候敲回车就能输出 Index: - ...

  6. ACDC

    acdc dcdc电源模块中大功率一般都是开关电源模式的,所以一般输入都是一个较宽的电源范围,体积也相对于变压器要小一些,效率高一些,但是纹波会偏大一些,如何选择就要看电路的需求来选择相应的方案

  7. spring注入简记

    我们知道对象是交给容器来管理的那么 init() destroy():可以在bean配置中设置对象初识化前执行和销毁后执行 int-delay=""表示是否延迟实例化即容器实例时还 ...

  8. hdu5406 CRB and Apple dp+两个LIS

    题意转换为:给定n个数,求两个最长的不相交的LIS. 先说经典题一个LIS的nlogn做法.枚举当前数,若比末尾数大,插入末尾,否则二分查找,插入合适位置. 通过此题,我们有了一个用树状数组或线段树+ ...

  9. java的重修之路

    一.内存管理 java里的声明分引用与基本数据类型. 数组: java里new一个对象数组为  person[] A; A = new person[4];  person[0] = new pers ...

  10. 纯css3样式属性制作各种图形图标

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...