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. js知识点 知识树 知识结构 (转载 学习中)

  2. js获取字符串的字节长度

    占用3个字节的范围 U+2E80 - U+2EF3 : 0xE2 0xBA 0x80 - 0xE2 0xBB 0xB3 共 115 个 U+2F00 - U+2FD5 : 0xE2 0xBC 0x80 ...

  3. Java Base64编码解码实现

    我尝试过两种方式:java自带的sun.misc的工具类,还有commons-codec.jar 1.sun.misc的工具类 String encoderStr = null; BASE64Enco ...

  4. CSS重置代码和常用公共代码

    发的发生的发生法士大夫撒打发士大夫

  5. html frames

    http://blog.sina.com.cn/s/blog_67697189010116o0.html *********************************************** ...

  6. 转!!java中Object转String

    Object转为String的几种形式 在java项目的实际开发和应用中,常常需要用到将对象转为String这一基本功能.本文将对常用的转换方法进行一个总结.常用的方法有Object.toString ...

  7. hiho_1055_刷油漆

    题目大意 一棵树,每个节点都有相应的value值.从根开始选择M个节点相互连通,使得这些节点的value值之和最大. 题目链接:[刷油漆][1] 题目分析 典型的树形dp,dp[i][j] 表示以节点 ...

  8. python操作mongodb之四cp数据库

    from pymongo import MongoClient #连接数据库 client=MongoClient('192.168.30.252',27017) #获取现有数据库的名称 client ...

  9. tabhost切换标签:Log中出现You must supply a layout_width attribute的解决方法

    谷歌.百度该问题,发现,除非是真的忘记添加layout_height或者layout_width属性值,对于布局文件没有语法问题但又难以发现问题所在的情况,从自己的经历和一个帖子的说明看到,该错误多半 ...

  10. js匿名函數

    (function($){})(jquery) == (function($){})(jQuery) 实际上是匿名函数 用于存放开发插件的代码 作用(非常有用): 这种写法的最大好处是形成闭包.在(f ...