Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11137    Accepted Submission(s): 3047

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
 

题意  求条形图中最大矩形的面积  输入给你条的个数  每一个条的高度hi   (下面等于也视为高)

仅仅要知道第i个条左边连续多少个(a)比他高   右边连续多少个(b)比他高  那么以这个条为最大高度的面积就是hi*(a+b+1);

可是直接枚举每个的话肯定会超时的   超时代码

#include<cstdio>
using namespace std;
const int N = 100005;
typedef long long ll;
ll h[N]; int n,wide[N];
int main()
{
while (scanf ("%d", &n), n)
{
for (int i = 1; i <= n; ++i)
scanf ("%I64d", &h[i]);
for (int i = 1; i <= n; ++i)
{
wide[i] = 1;
int k = i;
while (k > 1 && h[--k] >= h[i]) ++wide[i];
k = i;
while (k < n && h[++k] >= h[i]) ++wide[i];
}
ll ans = 0;
for (int i = 1; i <= n; ++i)
if (h[i]*wide[i] > ans) ans = h[i] * wide[i];
printf ("%I64d\n", ans);
}
return 0;
}

能够发现   当第i-1个比第i个高的时候   比第i-1个高的全部也一定比第i个高

于是能够用到动态规划的思想

令left[i]表示包含i在内比i高的连续序列中最左边一个的编号  
right[i]为最右边一个的编号

那么有   当h[left[i]-1]>=h[i]]时   left[i]=left[left[i]-1]  从前往后能够递推出left[i]

同理      当h[right[i]+1]>=h[i]]时   right[i]=right[right[i]+1]   从后往前可递推出righ[i]

最后答案就等于 max((right[i]-left[i]+1)*h[i])了

#include<cstdio>
using namespace std;
const int N = 100005;
typedef long long ll;
ll h[N];
int n, left[N], right[N];
int main()
{
while (scanf ("%d", &n), n)
{
for (int i = 1; i <= n; ++i)
scanf ("%I64d", &h[i]), left[i] = right[i] = i;
h[0] = h[n + 1] = -1;
for (int i = 1; i <= n; ++i)
while (h[left[i] - 1] >= h[i])
left[i] = left[left[i] - 1];
for (int i = n; i >= 1; --i)
while (h[right[i] + 1] >= h[i])
right[i] = right[right[i] + 1];
ll ans = 0;
for (int i = 1; i <= n; ++i)
if (h[i] * (right[i] - left[i] + 1) > ans) ans = h[i] * ll (right[i] - left[i] + 1);
printf ("%I64d\n", ans);
}
return 0;
}

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

  1. hdu 1506 Largest Rectangle in a Histogram ((dp求最大子矩阵))

    # include <stdio.h> # include <algorithm> # include <iostream> # include <math. ...

  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. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  4. 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 ...

  5. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  6. 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 ...

  7. 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 ...

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

                                                                                                       L ...

  9. HDU -1506 Largest Rectangle in a Histogram&&51nod 1158 全是1的最大子矩阵 (单调栈)

    单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一 ...

随机推荐

  1. 如何搭建Eclipse +Apache Tomcat配置Java开发环境

    Linux平台下如何搭建Eclipse +Apache Tomcat配置的Java开发环境 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处http://chenguang ...

  2. c#(asp.net) 如何计算两个日期之间相隔天数

    1.DateTime t1 = Convert.ToDateTime("2006-1-6"); DateTime t2 = Convert.ToDateTime("200 ...

  3. centos 5的yum源无法使用的解决方法( 转载)

    由于centos 5 已经停更.于是导致yum源也不能用了. 例如安装screen的时候提示 Determining fastest mirrors* base: denver.gaminghost. ...

  4. Unix操作系统的入门与基础

    http://dev2dev.cnblogs.com/archive/2005/10/10/251894.aspx Unix操作系统的入门与基础 与大家熟悉的Windows用户界面和使用习惯不同,Un ...

  5. 【Henu ACM Round#16 A】 Bear and Game

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 看看什么时候t[i]-t[i-1]>15. 输出t[i-1]+15就好. 不存在这样的i就输出min(t[n]+15,90) ...

  6. Activiti工作流(4):编写一个HelloWorld

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1.使用eclipse的activiti插件画流程图 在resource文件夹下新建一个工作流diagram 右键——new——other...— ...

  7. cogs 1755. 爱上捉迷藏

    1755. 爱上捉迷藏 ☆   输入文件:kadun.in   输出文件:kadun.out   简单对比时间限制:0.001 s   内存限制:2 MB [背景] 乃们都玩过赛尔号吧……,那有木有玩 ...

  8. 洛谷 P1013 进制位

    P1013 进制位 题目描述 著名科学家卢斯为了检查学生对进位制的理解,他给出了如下的一张加法表,表中的字母代表数字. 例如: + L K V E L L K V E K K V E KL V V E ...

  9. 整合struts2+spring+hibernate

     一.准备struts2+spring+hibernate所须要的jar包:        新建web项目并将jar包引入到project项目中. 二.搭建struts2环境        a.在 ...

  10. 新浪新闻按keyword抓取实例

    import urllib2 import requests #import MySQLdb import webbrowser import string import re from Beauti ...