你是不是飘了?骚年!

Problem 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

题目描述

给你一个直方图,求出最大的矩形面积。

思路

  • 常规的思路
    遍历每个点,查找这个点向左和向右一共可以扩展的长度。该点的面积就是高*长度。但是如果有相同的点连续或者其他情况,这样很容易超时 O(N*N).
  • dp
    也是遍历每个点,不同的是把每个点的课扩展的值存起来。这样就大大加快了速度。于是就开两个数组,一个存向左的长度,一个存向右的长度。
  • 单调栈
    思路和dp相似,栈内一直保持单调递增,每个栈元素存储一个高度和向左扩展的长度,遇到一个比栈顶小的数就将栈顶出栈,计算栈顶的面积(高度*长度)。

    • 关于栈元素的长度
      高度高的元素直接入栈,左长度为1
      第一个出栈的元素长度为1,以后相邻出栈的长度是左长度加上上一个出栈的长度。
      高度低的进栈之后,左长度为上一个出栈元素+1
  • 记得用 long long wa…..

AC代码:

dp
#include<iostream>
#include<string.h>
#define N 100005
#define ll long long
using namespace std;    //L记录左边第一个比i大的坐标
ll a[N], l[N], r[N];    //R记录右边最后一个比i大的坐标
int main (){            //L-R得到i可扩展的长度 //freopen("in.txt", "r", stdin);int n;while(scanf("%d", &n) && n) {for(int i = 1; i <= n; i++) {scanf("%lld", &a[i]);}l[1] = 1;r[n] = n;for(int i = 2; i <= n; i++) {   //找每个点左边第一个比i大的坐标 int t = i;while(t > 1 && a[i] <= a[t - 1])t = l[t - 1];l[i] = t;}for(int i = n - 1; i >= 1; i--) {   //找每个点最后一个比i大的坐标 int t = i;while(t < n && a[i] <= a[t + 1])t = r[t + 1];r[i] = t;}ll ans = 0;for(int i = 1; i <= n; i++) {ans = max(ans, (r[i] - l[i] + 1) * a[i]);   //遍历每个点,更新最大值 } printf("%lld\n", ans);}return 0;
}
单调栈
#include<iostream>
#include<stack>
#define N 100005
#include<stdio.h>
#define ll long long
using namespace std;
ll a[N], l[N];
struct ac{ll num, l;
};
int main (){//freopen("in.txt", "r", stdin);int n;while(scanf("%d", &n) , n) {stack<ac>sta;ll ans = 0;for(int i = 1; i <= n; i++) {scanf("%lld", &a[i]);}for(int i = 1; i <= n; i++) {if(sta.empty() || a[i] > sta.top().num){    //当栈为空或者栈顶元素小,直接进栈 sta.push((ac){a[i], 1});continue;}int cnt = 0;    //第一个出栈的元素,右面可扩展的长度为零 while(!sta.empty() && sta.top().num >= a[i]){//当栈顶元素大就出栈,并计算以该元素向左扩展的面积 ac t = sta.top();sta.pop();int len = t.l + cnt;    //求元素可扩展的长度 ans = max(ans, len * t.num);    //求元素可扩展的面积 cnt = len;  //更新cnt,下一个元素的右边可扩展的长度 }   sta.push((ac){a[i], cnt + 1});  //a[i]进栈,左边可以扩展的长度为最后出栈的长度加 1 }//最后处理栈,同上。 int cnt = 0;while(!sta.empty()) {ac t = sta.top();sta.pop();int len = t.l + cnt;ans = max(ans, len * t.num);cnt = len;}printf("%lld\n", ans);}       return 0;
}

HDU 1506 Largest Rectangle in a Histogram(dp、单调栈)相关推荐

  1. hdu 1506 Largest Rectangle in a Histogram 最大矩形

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1506 Largest Rectangle in a Histogram Time Limit: 20 ...

  2. *【HDU - 1506】【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. HDU 1506 解题报告 Largest Rectangle in a Histogram (单调栈)

    看题传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题意比较明显,就是找以某一个矩形为高的最大的矩形.这个题可以用单调栈来求解,需要注意的是如果从 ...

  4. HDU 1506 Largest Rectangle in a Histogram

    这个问题姑且也叫做最大子矩阵吧 给一个树状图,求一个最大面积的子矩阵 思路是这样的,对于每个单位矩阵,求出左边连续不比它低的矩阵的下标,放在l数组里 同样,再求出右边连续的不比它低的矩阵的下标 这样, ...

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

    题意:有n个高度不同的直方图,求直方图内最大的矩形面积. 分析: 1.若当前研究高度大于栈顶高度,则直接入栈.否则,边处理栈内所有高度大于等于当前高度的元素边出栈,在此过程中,边累加宽度边以当前栈顶元 ...

  6. 【POJ2559】Largest Rectangle in a Histogram(单调栈)

    problem 给你一堆宽度为1,高度不同的矩形条. 问你能框出一个最大矩形面积为多少. solution 如果矩形高度递增,那么答案为每个元素最多能向右扩展多少. 如果矩形高度比上一个小,那么该矩形 ...

  7. luoguSP1805,POJ2559-Largest Rectangle in a Histogram【单调栈】

    正题 POJ题目链接:http://poj.org/problem?id=2559 luogu评测记录:https://www.luogu.org/recordnew/lists?uid=52918& ...

  8. Largest Rectangle in a Histogram HDU - 1506 解题思路 单调栈

    原题目 Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a c ...

  9. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

最新文章

  1. P3830-[SHOI2012]随机树【数学期望,dp】
  2. 【LeetCode-面试算法经典-Java实现】【002-Add Two Numbers (单链表表示的两个数相加)】...
  3. TDengine安装
  4. 俄罗斯套娃(JOISC 2016 Day 1)
  5. 将Long类型的数通过UDP传输
  6. php ksc歌词,KSC字幕制作方法 KSC歌词编辑教程
  7. 面试官的窒息逼问: 到底什么是面向接口编程?
  8. win10笔记本电脑耳机没反应,耳机没声音的解决方法
  9. Scala(一):概述
  10. HTTP认证与https简介
  11. 原来连续查询mysql天数的sql语句这么简单呀
  12. 在1705年第一个电灯泡是如何被发明的?
  13. 遥感专业学习神经网络与深度学习过程中的想法
  14. 最简单的python语言程序设计_编程中最简单的语言Python,这样学或许更容易
  15. 出圈问题(java)-----n个人围成一圈,数到key或者key的倍数,出圈,问剩下的最后一个人原来的位置是多少?
  16. Linux系统编程 / 分析开源软件Triggerhappy
  17. 少年派 —— 之 读书
  18. 基于Java+springmvc+mysql+jquery实现企业员工管理系统
  19. javascript二维数组转置,如何使用JavaScript转置二维数组?
  20. PCB单双面板打样工程费跨入30元时代!

热门文章

  1. UIActionSheet
  2. MVC5+EF6 入门完整教程 总目录
  3. linux中内核中machine_desc,Linux-内核-学习笔记(13):移植三星官方内核
  4. 【数据结构与算法】之深入解析“将数据流变为多个不相交区间”的求解思路与算法示例
  5. HarmonyOS之变量可视化调试
  6. 【数据结构与算法】之N个数中有K个数可能的组合算法
  7. G6 图可视化引擎——入门教程——元素及其配置
  8. ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.
  9. 【Qt5.8】Qt5.8中串口类QSerialPort
  10. 【Linux系统编程】fork() 函数详解