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. 2012 #5 Gold miner

    Gold miner Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. Cheatsheet: 2013 12.01 ~ 12.16

    Mobile Performance Tuning On Android Interoperation Issues in Mixed C/C++/Objective-C Development We ...

  3. SQLServer: 解决“错误15023:当前数据库中已存在用户或角色

    解决SQL Server 2008 错误15023:当前数据库中已存在用户或角色,SQLServer2008,错误15023, 在使用SQL Server 2008时,我们经常会遇到一个情况:需要把一 ...

  4. [POJ1681]Painter's Problem(高斯消元,异或方程组,状压枚举)

    题目链接:http://poj.org/problem?id=1681 题意:还是翻格子的题,但是这里有可能出现自由变元,这时候枚举一下就行..(其实这题直接状压枚举就行) /* ━━━━━┒ギリギリ ...

  5. [数据结构与算法]栈Stack的多种实现

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. win 8 pip install 或者 pycharm 安装 paramiko 报错

    这是安装时报错的最后几行 creating build\temp.win-amd64-3.5\Release\build creating build\temp.win-amd64-3.5\Relea ...

  7. 2----lua的入门

    Lua的标识符 什么是标识符? 标识符就是你给对象,函数等取的名字 区分大小写 首字母可以使字母下划线和美元符号组成 Lua 的保留字 保留字(区分大小写) true false and or not ...

  8. iOS - UIImagePickerController

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImagePickerController : UINavigationController <NSCod ...

  9. [转载] 根据多年经验整理的《互联网MySQL开发规范》

    原文: http://weibo.com/p/2304181380b3f180102vsg5 根据多年经验整理的<互联网MySQL开发规范> 写在前面:无规矩不成方圆.对于刚加入互联网的朋 ...

  10. oracle dba 职责, 及个人需要掌握内容

    ORACLE DBA 职责, 基本相当于日常工作. 0. 数据库设计 1. 模式对象的创建与管理(table, index 等等) 2. 事物管理, 例如并发等 3. SQL 调优 只是针对SQL的 ...