Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29498   Accepted: 9539

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

  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个矩形,宽度都为1,给出N个矩形的高度,求由这N个矩形组成的图形包含的最大的矩形面积
 
  1. //单调递增栈
  2. #include<iostream>
  3. #define ll long long
  4. #include<stack>
  5. using namespace std;
  6. stack<ll>p; //栈里面存的是下标
  7. ll a[];
  8. ll n,s,top;
  9. int main()
  10. {
  11. while(~scanf("%lld",&n))
  12. {
  13. if(n==)
  14. break;
  15. while(!p.empty())
  16. p.pop();
  17. for(int i=;i<n;i++)
  18. scanf("%lld",&a[i]);
  19. a[n]=-;//为找比a[n-1]小的数准备,因为是递增栈,将a[n]设为最小值
  20. s=;
  21. for(int i=;i<=n;i++)
  22. {
  23. if(p.empty()||a[i]>=a[p.top()])//看题目要求是否要严格单调递增,这里只要求递增
  24. p.push(i);
  25. else
  26. {
  27. while(!p.empty()&&a[i]<a[p.top()])//找到第一个小于栈顶元素的数的下标
  28. {
  29. top=p.top();
  30. p.pop();//只在在出栈的过程中以a[top]为最小值更新面积s
  31. if(s<(i-top)*a[top])
  32. s=(i-top)*a[top];
  33. }
  34. p.push(top);//只将延伸到最左端的元素入栈,并且以最左端的元素的!坐标!为起点,找下一个比a[i]大的最长增区间
  35. a[top]=a[i];//修改该位置的值为a[i]
  36.  
  37. }
  38. }
  39. printf("%lld\n",s);
  40. }
  41. return ;
  42.  
  43. }

po'j2559 Largest Rectangle in a Histogram 单调栈(递增)的更多相关文章

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

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

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

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

  3. POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

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

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

                                                                                                       L ...

  5. POJ2559 Largest Rectangle in a Histogram —— 单调栈

    题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Lim ...

  6. HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

    题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...

  7. POJ2559 Largest Rectangle in a Histogram 单调栈

    题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...

  8. PKU 2559 Largest Rectangle in a Histogram(单调栈)

    题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...

  9. Largest Rectangle in a Histogram /// 单调栈 oj23906

    题目大意: 输入n,,1 ≤ n ≤ 100000,接下来n个数为每列的高度h ,0 ≤ hi ≤ 1000000000 求得最大矩阵的面积 Sample Input 7 2 1 4 5 1 3 34 ...

随机推荐

  1. Java程序设计17——多线程-Part-B

    5 改变线程优先级 每个线程执行都具有一定的优先级,优先级高的线程获得较多的执行机会,而优先级低的线程则获得较少的执行机会. 每个线程默认的优先级都与创建它的父线程具有相同的优先级,在默认情况下,ma ...

  2. asp.net mvc 5框架揭秘(文摘)

    第1章 asp.net + mvc 1.1.2 什么是MVC模式: model:对应用状态和业务功能的封装,同时包含数据和行为的领域模型. view:实现可视化界面的呈现并捕捉最终用户的交互操作. c ...

  3. Ansible 笔记 (2) - Ad-hoc 命令

    先使用 ansible-doc 获取帮助文档 [root@localhost ~]# ansible-doc ping > PING (/usr/lib/python2.7/site-packa ...

  4. Codeforces766B Mahmoud and a Triangle 2017-02-21 13:47 113人阅读 评论(0) 收藏

    B. Mahmoud and a Triangle time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  5. 机器学习—集成学习(Adaboost)

    一.原理部分: 二.sklearn实现: from sklearn.ensemble import AdaBoostClassifier from sklearn.datasets import lo ...

  6. spring获取webapplicationcontext,applicationcontext几种方法详解(转载)

    转载自  http://www.blogjava.net/Todd/archive/2010/04/22/295112.html 方法一:在初始化时保存ApplicationContext对象 代码: ...

  7. Hook ptrace 调试加入了ptrace函数的程序

    Hook ptrace 调试加入了ptrace函数的程序 #import <substrate.h> #if !defined(PT_DENY_ATTACH)#define PT_DENY ...

  8. [Erlang20]一起攻克Binary

    第一次看到Joe Armstong的<Erlang 程序设计>里面对Binary的描述时,觉得这个东西好复杂,语法这么奇特(我觉得是Erlang语法中最不好懂的部分); 然后在项目中:Bi ...

  9. Buffer Pool--SQL Server:Memory Manager 对象

    --=================================================================SELECT * FROM sys.sysperfinfoWHER ...

  10. Android intent 传值不更新的原因和解决办法

    当 Activity 的启动模式是 singleTask 或者 singleInstance 的时候.如果使用了 intent 传值,则可能出现 intent 的值无法更新的问题.也就是说每次 int ...