Largest Rectangle in a Histogram

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

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
 
Source
 
Recommend
LL   |   We have carefully selected several similar problems for you:  1505 1069 1087 1058 1176 
 
经典问题   广告印刷
有n个数ai。从中选出一段区间[L,R],使得(R-L+1)*min{a_L,…,a_R}最大
 
两个单调队列求每个点向两侧扩展到的最远距离
#include<cstdio>
#include<algorithm>
using namespace std;
#define LL long long
const int maxn= ;
#ifdef WIN32
#define lld "I64d"
#else
#define lld "lld"
#endif int a[maxn],b[maxn];
int l[maxn],r[maxn],tmp[maxn],q[maxn];
int n,m;
void work(int c[] ,int d[]) {
q[]=c[]; tmp[]=;
int head=,tail=;
for(int i=;i<=n;++i) {
while(head<=tail&&q[tail]>c[i]) d[tmp[tail--]]=i-;
q[++tail]=c[i];
tmp[tail]=i;
}
while(head<=tail) d[tmp[head++]]=n;
}
int main() {
while(scanf("%d",&n)&&n!=) {
LL ans=;
for(int i=;i<=n;++i)
scanf("%d",a+i),b[n-i+]=a[i];
work(a,r);
work(b,l);
for(int i=;i<=n;++i) tmp[i]=l[i];
for(int i=;i<=n;++i) l[n-i+]=n-tmp[i]+;
for(int i=;i<=n;++i) ans=max(ans,1ll*a[i]*(r[i]-l[i]+));
printf("%lld\n",ans);
}
return ;
}
 

HUD 1506 Largest Rectangle in a Histogram的更多相关文章

  1. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

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

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

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

  3. uva 1506 Largest Rectangle in a Histogram

    Largest Rectangle in a Histogram http://acm.hdu.edu.cn/showproblem.php?pid=1506 Time Limit: 2000/100 ...

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

  5. HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram

    http://acm.hdu.edu.cn/showproblem.php?pid=1506  || http://poj.org/problem?id=2559 Time Limit: 2000/1 ...

  6. HDU 1506 Largest Rectangle in a Histogram(区间DP)

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

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

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

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

                                                                                                       L ...

随机推荐

  1. Sniper OJ部分writeup

    0x00 shellcode pwn 因为题目直接有源码,我就不拖进IDA了,直接看代码 这是一个典型的栈溢出,我们只需要构造shellcode获得/bin/sh权限就可以得到flag.下面是所用脚本 ...

  2. NSLocale

      1.创建本地化对象 // 根据本地标识符创建本地化对象 NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier"e ...

  3. 【Java_多线程并发编程】基础篇—线程状态及实现多线程的两种方式

    1.Java多线程的概念 同一时间段内,位于同一处理器上多个已开启但未执行完毕的线程叫做多线程.他们通过轮寻获得CPU处理时间,从而在宏观上构成一种同时在执行的假象,实质上在任意时刻只有一个线程获得C ...

  4. linux各种终端类型的区别和概念

    1 pty(虚拟终端或伪终端): 当我们远程telnet到主机或使用xterm时不也需要一个终端交互么?是的,这就是虚拟终端pty(pseudo-tty). 2 tty(终端设备的统称):tty一词源 ...

  5. redux form

    纯粹使用react进行表单校验: class MyForm extends React.Component{ constructor(props){ super(props) this.onAddrC ...

  6. Mac单机模式安装启动Kafka

    1.下载kafka,网址: https://www.apache.org/dyn/closer.cgi?path=/kafka/2.0.0/kafka_2.12-2.0.0.tgz 2.移动tar包到 ...

  7. 【UOJ#51】【UR #4】元旦三侠的游戏(博弈论)

    [UOJ#51][UR #4]元旦三侠的游戏(博弈论) 题面 UOJ 题解 考虑暴力,\(sg[a][b]\)记录\(sg\)函数值,显然可以从\(sg[a+1][b]\)和\(sg[a][b+1]\ ...

  8. Hive 启动报错,需先启动元数据

    Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: Unable ...

  9. JAVA连接MYSQL8.0问题

    title: java连接mysql8.0问题 date: 2018-07-08 19:27:38 updated: tags: description: keywords: comments: im ...

  10. Django框架简介及模板Template,filter

    Django框架简介 MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View) ...