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. Best Financing(HD4833)

    每笔收入产生的收益是独立的. 计算所有点的收益率,累计. #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<al ...

  2. axis2 webservices 411错误解决办法

    错误:org.apache.axis2.AxisFault: Transport error: 411 Error: Length Required 可能会导致这个问题的原因: 1.访问地址经过端口映 ...

  3. PixelFormat 图像颜色的数据格式

    PixelFormat: (指定图像中每个像素的颜色数据的格式) Delphi                                        微软                    ...

  4. Delphi 服务操作

    unit Service; interface uses Windows,Messages,SysUtils,Winsvc,Dialogs; function  StartServices(Const ...

  5. Windows Azure 社区新闻综述(#69 版)

    欢迎查看最新版本的每周综述,其中包含有关云计算和 WindowsAzure 的社区推动新闻.内容和对话. 以下是过去一周基于您的反馈汇集在一起的内容: 文章.视频和博客文章 ·     使用 Azur ...

  6. Eclipse安装Weblogic插件

    1.启动Eclipse,打开window>References>server>Runtime server. 2.点击“add”按钮,添加新的web服务器. 注:上图Oracle是W ...

  7. 小窍门:变更Windows Azure Websites自带的node.exe版本

    这几天在玩node.js.Azure Websites天然支持node.js(还支持.net, php和python).   它对nodejs支持的原理是: IIS充当Web服务器,接收所有的请求,而 ...

  8. 网易云课堂_程序设计入门-C语言_第四周:循环控制_1素数和

    1 素数和(5分) 题目内容: 我们认为2是第一个素数,3是第二个素数,5是第三个素数,依次类推. 现在,给定两个整数n和m,0<n<=m<=200,你的程序要计算第n个素数到第m个 ...

  9. c++多态的简单理解

    基类中有一个抽象函数 很多个子类继承这个基类 要想使用将子类的对象赋值给基类对象,且使用基类对象可以调用到子类对象的方法,那么这个方法必须是在基类中抽象的,子类中实现的.如果基类中这个方法不是抽象方法 ...

  10. JavaScript 的DOM操作

    HTML DOM (文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树. Windows 对象操作 ...