Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15013    Accepted Submission(s): 4357

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

题解:让求最大矩形面积,宽为1,暴力超时

可以发现   当第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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x,y) scanf("%lf%lf",&x,&y)
#define P_ printf(" ")
const int MAXN=;
typedef long long LL;
LL a[MAXN];
int l[MAXN],r[MAXN];
int main(){
int N;
while(scanf("%d",&N),N){
for(int i=;i<=N;i++)scanf("%lld",&a[i]),l[i]=i,r[i]=i;
a[]=a[N+]=-;
for(int i=;i<=N;i++){
while(a[l[i]-]>=a[i])
l[i]=l[l[i]-];
}
for(int i=N;i>=;i--){
while(a[r[i]+]>=a[i])
r[i]=r[r[i]+];
}
LL ans=;
for(int i=;i<=N;i++){
ans=max(ans,(r[i]-l[i]+)*a[i]);
}
printf("%lld\n",ans);
}
return ;
}

Largest Rectangle in a Histogram(最大矩形面积,动态规划思想)的更多相关文章

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

    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左右处理边界的矩形问题)

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

  3. poj 2559 Largest Rectangle in a Histogram - 单调栈

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

  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. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  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. Largest Rectangle in a Histogram 常用技巧 stack的运用

    Largest Rectangle in a Histogram

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

                                                                                                       L ...

  9. Largest Rectangle in a Histogram HDU - 1506 (单调栈)

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

随机推荐

  1. linux dd命令

    dd 是 Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 1. 命令简介 dd 的主要选项: 指定数字的地方若以下列字符结尾乘以相应的数 ...

  2. Go语言AST尝试

    Go语言有很多工具, goimports用于package的自动导入或者删除, golint用于检查源码中不符合Go coding style的地方, 比如全名,注释等. 还有其它工具如gorenam ...

  3. ps -aux返回超过100%

    http://serverfault.com/questions/522922/cpu-more-than-100-in-ps-aux export NLS_LANG="SIMPLIFIED ...

  4. 500多条Linux信息

    http://www.cnblogs.com/zgqjymx/myposts.html?page=77 http://www.cnblogs.com/zgqjymx/tag/%E5%8E%9F%E5% ...

  5. 迁移到MSYS2 与 Qt 工具链注意的几个事情(注意链接顺序,并且人造mingw工具链所没有的局部midl.exe命令)

    Microsoft Visual Studio 2015社区版提供了强大的开发体验,且 Qt 提供了预编译版本.然而,由于客户提出兼容Windows XP ~ Windows 8.1 这样宽泛的环境要 ...

  6. Android 微信分享信息

    随着微信越来越火,越来越多的应用要求有分享到微信的功能.虽然有很多平台都帮集成有分享功能,比如友盟.但是个人觉得友盟集成的东西太多了,自己封装得太过分了,很多资源文件也要带进去,所以感觉不是怎么好,所 ...

  7. docker 运行挂载磁盘

    docker:/data# mkdir /awp docker:/data# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAM ...

  8. poj2909 || poj2262

    #include <stdio.h> #include <stdlib.h> #include<math.h> int isPri(int a, int b) { ...

  9. HTTP的头部

    if($this->GetHead("http-edition")=="HTTP/1.1") $httpv = "HTTP/1.1"; ...

  10. cf-公式专场

    A. Again Twenty Five! time limit per test 0.5 seconds memory limit per test 64 megabytes input stand ...