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

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

Sample Output

8
4000

Hint

Huge input, scanf is recommended.
题解:
给你N个宽都是一,长为a[[i]的矩形,然后让你求连续的几个矩形形成的最大面积(看图好理解题意);
对于这道题目,我们可以假设每一个矩形为最低的那个,然后依次往两边找,直到找到 比当前低的位置,
对于这些位置我们可以预处理存在l[i],r[i]数组里面;然后求最大值即可;
参考代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int maxn=1e5+;
int a[maxn],l[maxn],r[maxn];
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n)
{
memset(a,,sizeof(a));
for(int i=;i<n;i++) scanf("%d",&a[i]);
for(int i=,j=n-;i<n;i++,j--)
{
l[i]=i;r[j]=j;
while(l[i]>&&a[l[i]-]>=a[i]) l[i]=l[l[i]-];
while(r[j]<n-&&a[r[j]+]>=a[j]) r[j]=r[r[j]+];
}
long long max=,m;
for(int i=;i<n;i++)
{
m=(long long)(r[i]-l[i]+)*a[i];
if(max<m) max=m;
}
cout<<max<<endl;
}
return ;
}

最新文章

  1. Entity Framework 6 开发系列 目录
  2. Linux磁盘管理
  3. Hadoop Streaming框架使用(一)
  4. 数迹学——Asp.Net MVC4入门指南(2):添加一个控制器
  5. js中location.href的用法
  6. Cite a Website in Paper 论文中引用网页的格式
  7. 六步实现Rest风格的API
  8. dateTimePicker日期时间插件-----限定节假日调休的可选择性
  9. 暑假集训(1)第一弹 -----士兵队列训练问题(Hdu1276)
  10. MigLayout
  11. 在VS上配置OpenCV
  12. 在SQL 语句批量替换数据库字符串的方法
  13. 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。
  14. UiAutomator2.0 - 获取同行控件
  15. vue v-if控制显隐,页面加载出现闪现 v-cloak
  16. Codeforces 822E Liar dp + SA (看题解)
  17. C#程序终止问题CLR20R3解决方法
  18. Go语言之高级篇beego框架之model设计构造查询
  19. Hadoop安装教程_集群/分布式配置
  20. 绑定sql server数据库的用户与登录名

热门文章

  1. Git II: 操作远程Repository基础
  2. C++中对C的扩展学习新增语法——namespace
  3. [LC] 112题 路径总和(在二叉树里判断是否有哪条路径之和等于某个值)
  4. nyoj 113-字符串替换 (python replace, try ... except)
  5. Python3.7.1学习(三)求两个list的差集、并集与交集
  6. Bootstrap——导航条(navbar)
  7. Win32 COM组件 x Android Service (二)
  8. 嵌入式、C语言位操作的一些技巧汇总
  9. 阿里云:uwsgi--配置出错 bind(): Address already in use [core/socket.c line 769]
  10. Selenium网页自动登录项目(基于Python从0到1)