感觉动态规划都是玄妙的很,思维题吧(单调栈思维)

题解:让求最大矩形面积,宽为1,暴力超时

可以发现   当第i-1个比第i个高的时候   比第i-1个高的所有也一定比第i个高

于是可以用到动态规划的思想

令left[i]表示包括i在内比i高的连续序列中最左边一个的编号   right[i] 为最右边一个的编号

那么有   当 h[left[i]-1]>=h[i]]时   left[i]=left[left[i]-1]  从前往后可以递推出left[i]

同理      当 h[right[i]+1]>=h[i]]时   right[i]=right[right[i]+1]   从后往前可递推出righ[i]

最后答案就等于  max((right[i]-left[i]+1)*h[i]) 了;

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
/*题目题意:题目给了n个矩形的高度,问最大连续矩形的公共面积(底乘以这段连续矩形中
最短的高度),每个矩形的底是1
题目分析:我们可以枚举每一个矩形,把它当作最矮的矩形,剩下就差知道底了。
既然这个矩形是最矮的的那一个,那么它左边的矩形和右边的矩形的高度应该大于等于它!*/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
const int maxn=1e5+1000;
ll a[maxn],Left[maxn],Right[maxn];
int main()
{int n;while (~scanf("%d",&n)&&n){for(int i=1; i<=n; i++)scanf("%lld",&a[i]);Left[1]=1;Right[n]=n;for (int i=2; i<=n; i++) ///求出每个矩形左端非递减连续的下标{int t=i;while (t>1&&a[i]<=a[t-1])/**状态转移*/t=Left[t-1];Left[i]=t;}for (int i=n-1; i>=1; i--) ///求出每个矩形右端非递减连续的下标{int t=i;while (t<n&&a[i]<=a[t+1])t=Right[t+1];Right[i]=t;}ll ans=0;for (int i=1; i<=n; i++)ans=max(ans,(Right[i]-Left[i]+1)*a[i]);printf("%lld\n",ans);}return 0;
}

Largest Rectangle in a Histogram (动态规划+奇思妙想单调栈)求最大矩状图面积相关推荐

  1. *【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 ...

  2. HDU 1506 Largest Rectangle in a Histogram(dp、单调栈)

    你是不是飘了?骚年! Problem Description A histogram is a polygon composed of a sequence of rectangles aligned ...

  3. 【单调栈】Largest Rectangle in a Histogram(luogu-SP1805/poj 2559)

    Largest Rectangle in a Histogram luogu-SP1805 poj 2559 题目大意: 有n个并排的矩阵,高度为aia_iai​,宽度为1,现在让求包含于这些矩阵的并 ...

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

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

  5. poj 2559 Largest Rectangle in a Histogram 栈

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

  6. 【Python CheckiO 题解】Largest Rectangle in a Histogram

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

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

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

  8. POJ - 2559 Largest Rectangle in a Histogram(笛卡尔树,单调栈实现)

    题目链接:点击查看 题目大意:给出一排高度不同,宽度都为 1 的矩形,问拼起来后最大的矩形面积是多少 题目分析:普通做法是用单调栈直接维护,我一直觉得单调栈处理这种矩形问题都比较抽象,也可能是我太菜了 ...

  9. POJ2559 Largest Rectangle in a Histogram(单调栈)

    题意: 给出一组矩形的高,求最多能拼成矩形的最大面积,看图就很清楚了. 要点: 还是单调栈,现在有点感觉了,单调栈大概就是能求出当前值左右的比它大或小的数的范围.这题用高度作为单调栈,分别往左右找比当 ...

最新文章

  1. Rect 选择感兴趣区域
  2. buffsize 缓冲区的大小多少合适_6人餐桌尺寸规格一般是多少
  3. MFC实现Windows锁屏
  4. [渝粤教育] 西南科技大学 数据库应用 在线考试复习资料(3)
  5. pandas filter_数据分析之Pandas操作(2)
  6. 人群频率 | gnomAD数据库 (二) 后台数据的获取及质量评估
  7. spring架构初学者_完整的厨师和基础架构初学者指南
  8. oracle 11g 数据库恢复技术 --rman catalog
  9. 对客户端树控件的包装
  10. 深入研究:HTTP2的真正性能到底如何
  11. perl查看文件,提取指定信息输出到文件
  12. mac os安装SPSS26
  13. Oracle数字转汉字过程,oracle中如何将字符和数字转成中文
  14. 微信缓存dat怎么转图片_微信dat文件转换为图片
  15. 一个简单的socket套接字服务器,Python
  16. O - 期末考试之分等级
  17. 5月1日起正式实施!图解《关键信息基础设施安全保护要求》
  18. NLP领域论文笔记【研一下研二上】01
  19. Gym - 101612A 点亮数字
  20. 微信小程序+阿里物联平台+合宙Air724UG搭建无服务器物联系统(一)

热门文章

  1. C语言之字符数组在if{}里面赋值给char *引发的问题
  2. Android之jni编译出现no matching function for call to ‘_JNIEnv::GetJava(JNIEnv* , Java VM**)‘解决办法)‘
  3. windows之DNS7种资源记录和flushdns命令清除DNS缓存以及nslookup解析域名和ipconfig/all命令查看网络配置使用总结
  4. RC4加密解密java算法
  5. (10)C#偷懒的开始永无止境的循环?
  6. linux源码编译emqttd,emqtt编译及简单测试记录
  7. 来潮汕,这些食物不吃后悔一辈子...
  8. 教师节,老师们最大的愿望竟然是。。 | 今日最佳
  9. 颠覆传统,仅银行卡大小充电宝,10000mAh可登机,手机党出门随身必备!
  10. mapbox 导航_狂甩不掉,骑行最稳手机支架!一体式安装太方便,秒变单车导航仪...