HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
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
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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
typedef long long int ll;
ll l[maxn],r[maxn],h[maxn];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(!n)
break;
memset(l,,sizeof(l));
memset(r,,sizeof(r));
memset(h,,sizeof(h));
// memset(dp,0,sizeof(dp));
for(int i=;i<=n;i++){
scanf("%lld",&h[i]); }
l[]=;
r[n]=n; for(int i=;i<=n;i++){
int tmp=i;
while(tmp>&&h[i]<=h[tmp-])
tmp=l[tmp-]; l[i]=tmp;
} for(int i=n-;i>=;i--){
int tmp=i;
while(tmp<n&&h[i]<=h[tmp+])
tmp=r[tmp+];
r[i]=tmp;
}
ll ans=-;
for(int i=;i<=n;i++){
ll tmp=(r[i]-l[i]+)*h[i];
ans=max(ans,tmp);
}
printf("%lld\n",ans);
}
return ;
}
HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)的更多相关文章
- HDU 1506 Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- hdu 1506 Largest Rectangle in a Histogram ((dp求最大子矩阵))
# include <stdio.h> # include <algorithm> # include <iostream> # include <math. ...
- HDU 1506 Largest Rectangle in a Histogram(区间DP)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...
- 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 ...
- HDU 1506 Largest Rectangle in a Histogram set+二分
Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...
- 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 ...
- 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 ...
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
L ...
- HDU -1506 Largest Rectangle in a Histogram&&51nod 1158 全是1的最大子矩阵 (单调栈)
单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一 ...
随机推荐
- css的img移上去边框效果及CSS透明度
css的img移上去边框效果: .v_comment img{ height:36px; height:36px; float:left; padding:1px; margin:2px; borde ...
- Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]
WARNING: Failed to register in JMX: javax.naming.NamingException: Could not load resource factory cl ...
- 全局唯一标识符(GUID)
全局唯一标识符,简称GUID(发音为/ˈɡuːɪd/或/ˈɡwɪd/),是一种由算法生成的唯一标识,通常表示成32个16进制数字(0-9,A-F)组成的字符串,如:{21EC2020-3AEA-106 ...
- [转]java基础学习总结——equals方法
一.equals方法介绍 1.1.通过下面的例子掌握equals的用法 1 package cn.galc.test; 2 3 public class TestEquals { 4 public s ...
- OC基础--block
block与函数类似:1.可以保存代码 2.又返回值 3.有形参 4.调用方式一样 block的标志:^ 一.定义一个无参无返回值的block void (^myBlock)();--1.void 代 ...
- xml文件的读写操作
1.直接上代码:包含了xml文档的创建,读取xml文档,创建根节点,向根节点中添加子节点,保存xml文档----------先来张效果图: static void Main(string[] args ...
- publish_subscribe
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- Maven-本地安装
本文测试安装maven3 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Maven是 Apache 下的一个项目,目前最新版本是 3.0.4,我用的也是这个. 首先去官网下载 ...
- Cocos2d-X3.0 刨根问底(三)----- Director类源码分析
上一章我们完整的跟了一遍HelloWorld的源码,了解了Cocos2d-x的启动流程.其中Director这个类贯穿了整个Application程序,这章随小鱼一起把这个类分析透彻. 小鱼的阅读源码 ...
- bzoj 1493 暴力
我们可以枚举每个点,然后求出这个点到其余点最小消耗的代价,求出比t小的且距离最大的更新答案. /**************************************************** ...