Largest Rectangle in a Histogram

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 6
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
 
 
一道用DP来做的题目:
对于条形图的每一条列,如果比前面的小,那么把他们放在一起肯定比单独算面积要大,右边如此!!
 
对于直方图的每一个右边界,穷举所有的左边界。将面积最大的那个值记录下来。时间复杂度为O(n^2). 单纯的穷举在LeetCode上面过大集合时会超时。可以通过选择合适的右边界,做一个剪枝(Pruning)。观察发现当height[k] >= height[k - 1]时,无论左边界是什么值,选择height[k]总会比选择height[k - 1]所形成的面积大。因此,在选择右边界的时候,首先找到一个height[k] < height[k - 1]的k,然后取k - 1作为右边界,穷举所有左边界,找最大面积。
 
 #include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
int b[],c[];
__int64 a[];
int main()
{
int n,m,i;
__int64 max;
while(scanf("%I64d",&n)!=EOF&&n)
{
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n;i++)
{
b[i]=i;
c[i]=i;
}
for(i=;i<=n;i++)
{
m=i;
while(a[m-]>=a[i])
{
b[i]=b[m-];
m=b[m-];
if(m==)
break;
}
}
for(i=n-;i>=;i--)
{
m=i;
while(a[m+]>=a[i])
{
c[i]=c[m+];
m=c[m+];
if(m==n)
break;
}
}
max=a[]*(c[]-b[]+);
for(i=;i<=n;i++)
{
if(a[i]*(c[i]-b[i]+)>=max)
max=a[i]*(c[i]-b[i]+);
}
printf("%I64d\n",max);
}
return ;
}

Largest Rectangle in a Histogram(DP)的更多相关文章

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

    题意: 有一个柱状图,有N条柱子.每一条柱子宽度都为1,长度为h1...hN. 在这N条柱子所构成的区域中找到一个最大面积,每平方米3块钱,问最多赚多少钱. 输入: 1<=N<=10000 ...

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

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

  3. Largest Rectangle in a Histogram(HDU1506)

    Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...

  4. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  5. Largest Rectangle in a Histogram(hdu1506,单调栈裸题)

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

  6. Largest Rectangle in a Histogram(HDU 1506 动态规划)

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

  7. Largest Rectangle in a Histogram(最大矩形面积,动态规划思想)

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

  8. poj2559 Largest Rectangle in a Histogram(单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  9. ZOJ 1985 Largest Rectangle in a Histogram(刷广告)2010辽宁省赛

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21204 ...

随机推荐

  1. win10使用小技巧以及常见问题处理方案

    1.win10开机一直处于黑屏状态或者反复重启怎么处理? 处理小方法:开机按win+X组合件进入高级修复模式---选择修复开启模式---f4进入安全模式开机状态---管理控制面板---禁用或者卸载显卡 ...

  2. 改变jboss部署目录(虚拟目录)

    笔记: 在开发过程中,有时候我们希望将程序放在我们的源代码目录中,比如d:\code下,而不是放在jboss的deploy下,怎么办? 我们知道,jboss中集成了tomcat,因此跟tomcat一样 ...

  3. jQuery Mobile 可折叠

    可折叠的内容块 可折叠(Collapsibles)允许您隐藏或显示内容 - 对于存储部分信息很有用. 如需创建可折叠的内容块,请向某个容器分配 data-role="collapsible& ...

  4. 苗子sale record

    2016年7月11日 橘白/阳光 发广州 220  中通 719373064939 成功2016年7月11日 横纹RA    发潮州 250  中通 719373064940 成功2016年7月18日 ...

  5. MySQL binlog基本操作

    常用操作: 1. 设置启用binlog log-bin = binlog 2. 设置全备和增量备份 #crontab -e * 0 * * 7 /usr/bin/mysqldump mybinlog ...

  6. TP框架,根据当前应用状态对应的配置文件

    index.php define('APP_STATUS','website'); /ThinkPHP/Library/Think/Dispatcher.class.php /** * 应用程序初始化 ...

  7. 设置secureCRT的鼠标右键为弹出文本操作菜单功能

    options菜单下的 global options 页面的 terminal 中的 mouse 子菜单对 paste  on  right button 的选项取消勾选即可.

  8. 类Arrays

    package p2; import java.util.Arrays; import java.util.List; public class ArraysDemo { public static ...

  9. SharePoint 2013开发入门探索(二)- 列表操作

    我们如何用代码对SharePoint列表做些例如增删改查的操作呢?如果您的程序可以部署到服务器上,就可以使用 服务器对象模型,因为服务器对象模型提供的功能最多,限制最少:否则可能要选择客户对象模型等其 ...

  10. javascript鸭式辩型法实现接口

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...