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 ;
}
 

最新文章

  1. 用ffmpeg合并音频文件
  2. 牛B的调试工具:OzCode
  3. 2015ACM/ICPC亚洲区长春站 G hdu 5533 Dancing Stars on Me
  4. java 集合(Set1)
  5. Linux下串口与工业协议的开发
  6. 一种c#深拷贝方式完胜java深拷贝(实现上的对比)
  7. C#中关于webconfig的读写
  8. [AngularJS] Services, Factories, and Providers -- Service vs Factory
  9. 关于html标签元素的data-*属性
  10. django note
  11. Hibernate与Jpa的关系(2)
  12. C语言初探
  13. mybatis-generator自动生成代码插件使用详解
  14. (三)apache的安装与配置
  15. q次询问,每次给一个x,问1到x的因数个数的和。
  16. HashMap实现原理分析--面试详谈
  17. 前端基础之Bootstrap
  18. Homebrew设置代理
  19. Struts2拦截器和标签
  20. mysql 如何找出两张表之间的关系

热门文章

  1. jquery 获取tbody下的第二个tr 及多级标签
  2. 【dp 贪心】bzoj4391: [Usaco2015 dec]High Card Low Card
  3. 如何用纯 CSS 创作阶梯文字特效
  4. pandas模块(很详细归类),pd.concat(后续补充)
  5. python安装numpy模块
  6. Day12装饰器
  7. POJ 1273 Drainage Ditches(最大流Dinic 模板)
  8. js-dom-EventUtil
  9. Java-改变Class
  10. appium+python自动化-微信公众号webview操作