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. Ubuntu系统下面软件安装更新命令

    在ubuntu服务器下安装包的时候,经常会用到sudo apt-get install 包名 或 sudo pip install 包名,那么两者有什么区别呢? 1.区别 pip用来安装来自PyPI( ...

  2. 转:union 联合体(共用体)

    转自:http://blog.csdn.net/xiao3404/article/details/22276485 2.共用体 2.1共用体的概念 共用体是一种构造类型的数据结构.在一个“共用体”内可 ...

  3. CSS小结

    一.1. css必须写在<head></head>里面的<style></style>里面 2. css 由选择器 + 规则组成, 规则由属性和值组成 ...

  4. Git入门及上传项目到github中

    最近需要将课设代码上传到Github上,之前只是用来fork别人的代码. 这篇文章写得是windows下的使用方法. 第一步:创建Github新账户 第二步:新建仓库 第三部:填写名称,简介(可选), ...

  5. 谈谈JPA-03-基本注解

    @Entity @Entity 标注用于实体类声明语句之前,指出该Java 类为实体类,将映射到指定的数据库表.如声明一个实体类 Customer,它将映射到数据库中的 customer 表上. @T ...

  6. Compound Interest Calculator2.0

    Compound Interest Calculator2.0 1.如果按照单利计算,本息又是多少呢? 2.假如30年之后要筹措到300万元的养老金,平均的年回报率是3%,那么,现在必须投入的本金是多 ...

  7. 关于C语言和汇编语言相互嵌套调用

    1.C嵌套汇编 首先说一下关于GCC编译嵌有汇编语言的c语言吧,GCC编译的汇编语言不是我们上课时学的Intel x86汇编,而是AT&T汇编,两者的区别可以查看<Gcc使用的内嵌汇编语 ...

  8. Objective-C:@class和#import

    @class和#import是OC中引用一个类的两种方式,其区别在于: #import相当于把被引用文件的内容拷贝到目标文件,这会包含被引用类的所有信息,包括被引用类的变量和方法(会降低编译性能 ): ...

  9. Word文档增加快捷键

  10. matlab report generator

    Programmatic report creation Create report content Mlreportgen.dom.Document.append Mlreportgen.dom.P ...