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. C# 动软生成器对应的Access数据库操作类DbHelperOleDb

    using System;using System.Collections;using System.Collections.Specialized;using System.Data;using S ...

  2. TDDL DataSource

    TDDL DataSource 分为 AtomDataSource GroupDatasource 他们两者没有依赖关系, 都实现了 JDBC 规范, 可以作为独立的 datasource 单独使用 ...

  3. JavaWeb技术(二):DAO设计模式

    1. DAO全称:Data Access Object , 数据访问对象.使用DAO设计模式来封装数据持久化层的所有操作(CRUD),使得数据访问逻辑和业务逻辑分离,实现解耦的目的. 2. 典型的DA ...

  4. ST算法

    作用:ST算法是用来求解给定区间RMQ的最值,本文以最小值为例 举例: 给出一数组A[0~5] = {5,4,6,10,1,12},则区间[2,5]之间的最值为1. 方法:ST算法分成两部分:离线预处 ...

  5. Nodejs Express下引入本地文件的方法

    Express的结构如下: |---node_modules------用于安装本地模块.     |---public------------用于存放用户可以下载到的文件,比如图片.脚本文件.样式表 ...

  6. Linux 配置nginx

    1.首先安装依赖包: # yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre ...

  7. echarts绘制甘特图

      在setoption之后添加这段代码: window.addEventListener('resize', function () { myChart.resize();   }); 图表就能随着 ...

  8. json改造优化无刷新分页

    index.html dd

  9. iOS遍历相册中的图片

    //获取相册的所有图片 - (void)reloadImagesFromLibrary { self.images = [[NSMutableArray alloc] init]; dispatch_ ...

  10. 正确获得android设备的IP地址

    网上此类获得android设备IP地址相关的文章有不少,有一篇是比较通用的,但有一个问题:有些设备默认的是IPv6的地址,那段代码获得的就是IPv6的地址.但这显然不是我们想要的,我们需要的是IPv4 ...