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
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. const int N = 100005;
  5. struct Elem
  6. {
  7. int height;
  8. int count;
  9. };
  10. Elem stack[N];
  11. int top;
  12. int main()
  13. {
  14. int height, n;
  15. long long ans, tot, tmp;
  16. while (scanf("%d", &n) != EOF && n)
  17. {
  18. top = 0;
  19. ans = 0;
  20. for (int i = 0; i < n; ++i)
  21. {
  22. scanf("%d", &height);
  23. tmp = 0;
  24. while (top > 0 && stack[top - 1].height >= height)
  25. {
  26. tot = stack[top - 1].height * (stack[top - 1].count + tmp);
  27. if (tot > ans) ans = tot;
  28. tmp += stack[top - 1].count;
  29. --top;
  30. }
  31. stack[top].height = height;
  32. stack[top].count = 1 + tmp;
  33. ++top;
  34. }
  35. tmp = 0;
  36. while (top > 0)
  37. {
  38. tot = stack[top - 1].height * (stack[top - 1].count + tmp);
  39. if (tot > ans) ans = tot;
  40. tmp += stack[top - 1].count;
  41. --top;
  42. }
  43. printf("%lld\n", ans);
  44. }
  45. return 0;
  46. }

POJ 2559 Program C的更多相关文章

  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

    http://poj.org/problem?id=2559 题意:就是找出可以完整连接的最大的矩形面积. 思路:找出单独的一块矩形,往两边延伸,记录两边的比他高的矩形是在哪个位置,然后最右的位置减去 ...

  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 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

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

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

  8. 【POJ 2559】 Largest Rectangle in a Histogram

    [题目链接] http://poj.org/problem?id=2559 [算法] 单调栈 [代码] #include <algorithm> #include <bitset&g ...

  9. 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)

    题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...

随机推荐

  1. C++——对象和类

    最重要的OOP特性: *抽象: *封装和数据隐藏: *多态: *继承: *代码的可重用性: 一.抽象和类 1.类型 指定基本类型完成了三项工作:1).决定数据对象需要的内存数量:2).决定如何解释内存 ...

  2. 20160805_CentOS6_控制台切换

    1. Ctrl + Alt + F1~F6 Ctrl + Alt + F1 是 图形界面(如果装了的话),后面的是 控制台界面 2. 3.

  3. ifstream,fstream 读写文件问题,read读取字节不够

    从网上下的一个读写bmp文件的接口使用了ifstream和fstream. 发现了在使用read读的时候,读取的字节达不到期望的size. 原因是打开文件的时候使用了ios::in 或ios::out ...

  4. git 10.8

    git clone xxxx.git生成一个本地的文件夹acd agit checkout -b abcgit checkout mastergit pull然后数据全部由更新 但是是远程的更新 不能 ...

  5. python核心编程第六章练习6-8

    6-8.列表.给出一个整型值,返回代表该值得英文,比如输入89会返回“eight-nine”.附加题:能够返回符合英文语法规律的新式,比如输入89会返回“eighty-nine”.本练习中的值假定在0 ...

  6. jq手风琴---点击时列表的左边距逐渐减小

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. validate插件:验证密码没有空格 用户名是5-10位 至少包含数字和大小写字母中的两种字符

    //校验密码是否含有空格 jQuery.validator.addMethod("notblank", function(value, element) { var pwdblan ...

  8. js高级程序设计(四)变量、作用域和内存问题

    基本类型和引用类型的值 ECMAScript 变量可能包含两种不同数据类型的值:基本类型值和引用类型值.基本类型值指的是 Undefined . Null . Boolean . Number 和 S ...

  9. css--block formatting context

    block formatting context(块级格式化上下文) 如何产生BFC:当一个HTML元素满足下面条件的任何一点,都可以产生Block Formatting Context: float ...

  10. Qt之窗口动画(下坠、抖动、透明度)

    简述 前面几节中我们介绍了关于动画的基本使用,有属性动画.串行动画组.并行动画组.这节我们来实现一些特效,让交互更顺畅. 简述 示例 效果 源码 更多参考 示例 下面,我们以geometry.pos. ...