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题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一 ...
随机推荐
- linux下定时任务的使用
使用方法 执行crontab -e命令会进入一个可编辑界面,在该界面中我们可以制定定时任务,然后保存退出(wq) 格式如下: 由于直接运行编辑命令后只是一个空白界面,不够友好,所以建议使用以下方式来增 ...
- WEB前端开发规范
WEB前端开发规范 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档.本文档如有不对或者不合适的地方请及时提出, 经讨论决定后方可更改. 基本 ...
- [代码片段]javascript检查图片大小和格式
function checkImgType(input) { var this_ = document.getElementsByName('imgFile')[0]; var filepath = ...
- QQ面向对象设计
通讯项目--仿QQ聊天程序 详细设计说明书 一.引言 此项目为验证Jav ...
- (转)Java并发编程:volatile关键字解析
转:http://www.cnblogs.com/dolphin0520/p/3920373.html Java并发编程:volatile关键字解析 volatile这个关键字可能很多朋友都听说过,或 ...
- 使用nginx绑定域名,代理gitlab
默认情况下,gitlab通过自带的unicorn来充当web页面的,不用nginx也可以,这里我们使用nginx代理vim /etc/yum.reos.d/nginx.repo # 编辑nginx.r ...
- javascript this 详解
前言 Javascript是一门基于对象的动态语言,也就是说,所有东西都是对象,一个很典型的例子就是函数也被视为普通的对象.Javascript可以通过一定的设计模式来实现面向对象的编程,其中this ...
- 小菜鸟学 MQ(三)
创建程序测试MQ 1,创建生产者 package com.robert; import java.util.Hashtable; import java.util.Map; import javax. ...
- 编译java文件,出错:Failed to establish a connection with the target VM
helloword程序,所有java学习人员人生第一个程序,哎妈,基础太差,出错 public class Helloword{ public Helloword() { public static ...
- Spring学习4-面向切面(AOP)之aspectj注解方式
一.简介 1.AOP用在哪些方面:AOP能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任,例如事务处理.日志管理.权限控制,异常处理等,封装起来,便于减少系统的重复代码,降低模块间的耦合 ...