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. echo输出到stderr

    echo "Your error message here" >&2 This is a normal echo (which goes to stdout), ho ...

  2. jquery动态样式操作

    获取与设置样式 获取class和设置class都可以使用attr()方法来完成.例如使用attr()方法来获取p元素的class,JQuery代码如下: 1 var p_class = $(" ...

  3. Sqlserver2008日志压缩

    SqlServer2008日志压缩语句如下: USE [master] GO ALTER DATABASE DBName SET RECOVERY SIMPLE WITH NO_WAIT GO ALT ...

  4. nancy的诊断2

    接上文. 一 再看看第二个面板内容 点击带文号 的  Interactive Diagnostics 这个面板 如下 上面的是诊断程序,默认是系统提供的测试用的诊断程序.  你可以 通过实现IDiag ...

  5. hdu 5693 朋友 博弈

    朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Descr ...

  6. MySQl的几个配置项

    对对于MySQL的日志功能,我们可以完全自己控制到底写还是不写.一般来说,binlog我们一般会开启,而对于慢查询我们一般会在开发的时候调试和观 察SQL语句的执行速度.但今天发现一个问题.在使用sh ...

  7. LINQ之路 5:LINQ查询表达式

    书写LINQ查询时又两种语法可供选择:方法语法(Fluent Syntax)和查询表达式(Query Expression). LINQ方法语法的本质是通过扩展方法和Lambda表达式来创建查询.C# ...

  8. [转]-Android Studio 快捷键整理分享-SadieYu

    文章编辑整理:Android Studio 中文组 - SadieYu Alt+回车 导入包,自动修正 Ctrl+N   查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L  格式化代码 ...

  9. [转载] 高效 MacBook 工作环境配置

    原文: http://mp.weixin.qq.com/s?__biz=MjM5NzMyMjAwMA==&mid=208231200&idx=1&sn=8a76ddc56c1f ...

  10. [转载] C++ STL string的Copy-On-Write技术

    原文: http://coolshell.cn/articles/12199.html stl的string是经过严格优化的, 深入理解对以后编程过程中应用string非常有益处, 感谢左耳朵耗子的精 ...