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, where0<=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

  1. 7 2 1 4 5 1 3 3
  2. 4 1000 1000 1000 1000
  3. 0

Sample Output

  1. 8
  2. 4000

Hint

Huge input, scanf is recommended.
题解:
给你N个宽都是一,长为a[[i]的矩形,然后让你求连续的几个矩形形成的最大面积(看图好理解题意);
对于这道题目,我们可以假设每一个矩形为最低的那个,然后依次往两边找,直到找到 比当前低的位置,
对于这些位置我们可以预处理存在l[i],r[i]数组里面;然后求最大值即可;
参考代码:
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<cstdlib>
  6. #include<algorithm>
  7. #include<queue>
  8. #include<deque>
  9. #include<stack>
  10. #include<set>
  11. #include<vector>
  12. #include<map>
  13. using namespace std;
  14. typedef long long LL;
  15. const int maxn=1e5+;
  16. int a[maxn],l[maxn],r[maxn];
  17. int main()
  18. {
  19. int n;
  20. while(scanf("%d",&n)!=EOF&&n)
  21. {
  22. memset(a,,sizeof(a));
  23. for(int i=;i<n;i++) scanf("%d",&a[i]);
  24. for(int i=,j=n-;i<n;i++,j--)
  25. {
  26. l[i]=i;r[j]=j;
  27. while(l[i]>&&a[l[i]-]>=a[i]) l[i]=l[l[i]-];
  28. while(r[j]<n-&&a[r[j]+]>=a[j]) r[j]=r[r[j]+];
  29. }
  30. long long max=,m;
  31. for(int i=;i<n;i++)
  32. {
  33. m=(long long)(r[i]-l[i]+)*a[i];
  34. if(max<m) max=m;
  35. }
  36. cout<<max<<endl;
  37. }
  38. return ;
  39. }

POJ 2559 Langest Rectangle in a Histogame的更多相关文章

  1. [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

    [POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...

  2. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

  3. stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

    题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...

  4. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  5. POJ 2559 Largest Rectangle in a Histogram -- 动态规划

    题目地址:http://poj.org/problem?id=2559 Description A histogram is a polygon composed of a sequence of r ...

  6. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    [题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...

  7. POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈

    嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...

  8. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  9. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

随机推荐

  1. IDEA Debug 无法进入断点的解决方法

    文章来源: https://studyidea.cn/idea_breakpoint_not_use 前言 某个多模块项目中使用多个版本的 Spring,如 Spring 4,Spring 5,在使用 ...

  2. git Lab ssh方式拉取代码失败

    gitLab在linux上已经安装好了, 在配置项目的时候报如下异常 使用http方式没问题, 但是用ssh方式设置repository URL 提示资源库不存在. returned status c ...

  3. nyoj 412 Same binary weight ()

    Same binary weight 时间限制:300 ms  |  内存限制:65535 KB 难度:3   描述 The binary weight of a positive  integer ...

  4. Spring Boot2 系列教程(二十六)Spring Boot 整合 Redis

    在 Redis 出现之前,我们的缓存框架各种各样,有了 Redis ,缓存方案基本上都统一了,关于 Redis,松哥之前有一个系列教程,尚不了解 Redis 的小伙伴可以参考这个教程: Redis 教 ...

  5. Jmeter---第一天配置中文环境、安装jmeter插件

    一:安装就不在赘述,百度有很多优秀的文章.接下来开始我自己的学习笔记 二:设置JMETER,切换中文环境. 首先打开jmeter的安装目录,找到bin文件目录中的jmeter.propertie 打开 ...

  6. 看完这篇还不会用Git,那我就哭了!

    你使用过 Git 吗?也许你已经使用了一段时间,但它的许多奥秘仍然令人困惑. Git 是一个版本控制系统,是任何软件开发项目中的主要内容.通常有两个主要用途:代码备份和代码版本控制.你可以逐步处理代码 ...

  7. java中的基本数据类型转换

    Java 中的 8 种基本数据类型,以及它们的占内存的容量大小和表示的范围,如下图所示: 重新温故了下原始数据类型,现在来解释下它们之间的转换关系. 自动类型转换 自动类型转换是指:数字表示范围小的数 ...

  8. 设计模式之观察者模式--PHP

    列举一个场景:下班之后回家,打开家门,开始做饭,之后睡觉 以上场景如果按照传统的开始方式就是封装一个用户类,里面有回家方法,打开门方法,做饭方法,睡觉方法,之后在外面依次调用. 假设你代码开发完了,这 ...

  9. Git实战指南----跟着haibiscuit学Git(第一篇)

    笔名:  haibiscuit 博客园: https://www.cnblogs.com/haibiscuit/ Git地址: https://github.com/haibiscuit?tab=re ...

  10. Linux基本架构

    Linux linux设计思想 1.程序应该小而专一,程序应该尽量的小,且只专注于一件事上,不要开发那些看起来有用但是90%的情况都用不到的特性: 2.程序不只要考虑性能, 程序的可移植性更重要,sh ...