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. 深入浅出RxJava(一:基础篇)

    RxJava正在Android开发者中变的越来越流行.唯一的问题就是上手不容易,尤其是大部分人之前都是使用命令式编程语言.但是一旦你弄明白了,你就会发现RxJava真是太棒了. 这里仅仅是帮助你了解R ...

  2. Android动画之translate(位移动画)

    上一篇文章讲了 Android的左右滑动切换,实现过程是非常简单,一些新手可能会向深入了了解Activity切换的原理,下面主要对左右滑动进行深入的探讨,并以项目中的一个切换效果来进一步了解. Act ...

  3. git的作用和原理(待续)

    先选定一个目录作为Git仓库,假定是/srv/sample.git,在/srv目录下输入命令: $ sudo git init --bare sample.git Git就会创建一个裸仓库,裸仓库没有 ...

  4. Android_SDK的常用命令

    一.配置环境变量 要想使用这些命令,就必须先配置环境变量.  将android-sdk-windows目录下的platform-tools目录和tools目录配置到path环境变量中 二.adb命令 ...

  5. linux下在jar包中找类是否存在

    find /usr/lib -name "*.jar" -exec grep -Hsli 类名 {} \;

  6. ActiveX控件(MFC篇)

    目录 第1章 VC++6.0创建控件    1 1.1 目标    1 1.1.1 方法    1 1.1.2 属性    1 1.1.3 事件    1 1.2 创建项目    2 1.3 项目结构 ...

  7. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  8. javaSE基础之记事本编程

    首先安装好jdk和jre,之后进行如下操作: 1. 将代码记事本---->cmd--->javac 文件名.java ----->java 文件名 如图: 2. 关于记事本文件属性的 ...

  9. 初学java之try-catch-finally语句的实例

    /* try - catch语句的例子,模拟向货船上装载集装箱 ,如果货船超重,那么货船认为这是一个异常,将拒绝装载集装箱, 但无论是否发生异常,货船都需要正点起航. */ package st; c ...

  10. C# string 数组 每个元素 加上单引号,每一个都被包含在单引号内

    在拼接SQL的时候经常会遇到此类问题,尤其是in查询的时候,内容是一段 单引号的 字符的时候 strWhere += " a.EC101_WRYBH  IN (" + string ...