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

 

解题报告:

 

    单调栈模板!此题用动态规划亦可解。题目大意就是求矩形的可最大面积

ac代码1:(因为第一次用单调栈所以代码十分丑陋、、、)

#include<bits/stdc++.h>#define ll long longusing namespace std;
const int MAX = 100000 + 5 ;
const int INF = 0x3f3f3f3f;
int top;
stack <ll> sk,sk2;
ll a[MAX];
ll zuo[MAX],you[MAX];
ll maxx;
int main()
{int n;while(scanf("%d",&n) ) {if( n==0 ) break;maxx=0;while(!sk.empty() ) sk.pop();while(!sk2.empty() ) sk2.pop();top = -1;for(int i = 0; i<n; i++) {scanf("%lld",&a[i]);}//递增栈 找左侧的最小元素 sk.push(0);zuo[0]=-1;for(int i = 1; i<n; i++) {if(sk.empty() ) {zuo[i]=-1;sk.push(i);}else {while(!sk.empty() && a[sk.top()]>=a[i] ) {sk.pop();}if(sk.empty() ) {zuo[i]=-1;sk.push(i);}else {zuo[i]=sk.top();sk.push(i);}}}sk2.push(n-1);you[n-1]=n;for(int i = n-2; i>=0; i--) {if(sk2.empty() ) {you[i]=n;sk2.push(i);}else {while(!sk2.empty() && a[sk2.top()]>=a[i] ) {sk2.pop();}if(sk2.empty() ) {you[i]=n;sk2.push(i);}else {you[i]=sk2.top();sk2.push(i);}}}for(int i = 0; i<n; i++) {maxx=max(maxx,a[i]*(you[i]-zuo[i]-1) );}printf("%lld\n",maxx);}return 0 ;
}

ac代码2:(动态规划)

 

如果确定了长方形的左端点L和右端点R,那么最大可能的高度就是min{hi|L <= i < R}。

L[i] = (j <= i并且h[j-1] < h[i]的最大的j)

R[i] = (j > i并且h[j] > h[i]的最小的j)

#include <stdio.h>#define MAX_N 100000int n;
int h[MAX_N];
int L[MAX_N], R[MAX_N];
int stack[MAX_N];long long max(long long a, long long b){return (a > b) ? a : b;
}void solve(){//计算Llong long ans = 0;int t = 0;int i;for (i = 0; i < n; ++i){while (t > 0 && h[stack[t-1]] >= h[i]) t--;L[i] = (t == 0) ? 0 : (stack[t-1] + 1);stack[t++] = i;}//计算Rt = 0;for (i = n - 1; i >= 0; --i){while (t > 0 && h[stack[t-1]] >= h[i]) t--;R[i] = (t == 0) ? n : stack[t-1];stack[t++] = i;}for (i = 0; i < n; ++i){ans = max(ans, (long long)h[i] * (R[i] - L[i]));}printf("%lld\n", ans);
}int main(void){int i;while (scanf("%d", &n) != EOF && n != 0){for (i = 0; i < n; ++i)scanf("%d", &h[i]);solve();}return 0;
}

ac代码3:(动态规划)

/*
每一个图形都有一个h[i],l[i],r[i]!其中l[i]存的是左数至少比他高的图形的序数,r[i]存的是右数至少比他高的图形的序数!
剪枝 while(h[r[i]+1]>=h[i]) {r[i]=r[r[i]+1];}特别重要!看右面有没有比他大的,
有就看右面那个r[]存的序数是多少,一步一步最终找到连续的长方形!
*/
#include<cstdio>
#include<iostream>
const int N=110000;
using namespace std;
long long h[N],l[N],r[N];
int main()
{long long n;int i;while(scanf("%lld",&n)&&n){for(i=1;i<=n;i++){scanf("%lld",&h[i]);l[i]=r[i]=i;}h[0]=h[n+1]=-1;for(i=1;i<=n;i++){while(h[l[i]-1]>=h[i]){l[i]=l[l[i]-1];//这个体现动态规划的思想,存l[l[i]-1]而不是l[i]-1}//这样就可以节省其中很多的步骤!根据动态规划原理这一找}for(i=n;i>=1;i--){while(h[r[i]+1]>=h[i]){r[i]=r[r[i]+1];}}long long maxs=-5,flag;for(i=1;i<=n;i++){flag=(r[i]-l[i]+1)*h[i];if(flag>maxs){maxs=flag;}}printf("%lld\n",maxs);}
}

ac代码4:(单调栈模板)

#include<bits/stdc++.h>using namespace std;typedef long long ll;
const int N = 100000 + 100;stack<int> S;
ll h[N];
int R[N],L[N];int main(){int n;while(~scanf("%d",&n) && n){for(int i=0 ;i<n ;i++)  scanf("%I64d",&h[i]);while(S.size()) S.pop();for(int i=0 ;i<n ;i++){while(S.size() && h[S.top()] >= h[i]) S.pop();if(S.empty())     L[i] = 0;else              L[i] = S.top() + 1;S.push(i);}while(S.size()) S.pop();for(int i=n-1 ;i>=0 ;i--){while(S.size() && h[S.top()] >= h[i]) S.pop();if(S.empty()) R[i] = n;else          R[i] = S.top();S.push(i);}ll ans = 0;for(int i=0 ;i<n ;i++){ans = max(ans,h[i] * (R[i] - L[i]));}printf("%I64d\n",ans);}return 0;
}

 

总结:

 

        1.单调栈的题 读数最好从1开始,不然就会出现ac代码4中S.top()+1这种不优雅的情况。

        2.就算你是从0开始读的,但如果没有比L(i)小的,也要赋值坐标成0!因为你如果左边单调栈一次,右边单调栈一次,最后算差值的时候还要再-1(比如ac代码1),也会不美观。

        3.综上所述,还是从i=1开始读比较好。。

        4.注意数据是需要longlong的!!

        5.下附单调栈模板(核心):

for(int i=1 ;i<=n ;i++){while(S.size() && h[S.top()] >= h[i]) S.pop();if(S.empty())     L[i] = 0;else              L[i] = S.top();S.push(i);}

        6.对上述模板有两个地方是可以更改的,一个是   h[S.top()] >= h[i]   的等于号,另一个是栈中存的数据,有时是存下标(位置),如此题,还有的情况是存值。这一点依题目情况而定。

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442085.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pdf转图片记录

1 winform将pdf转图片&#xff0c;有案例&#xff0c;连接为&#xff1a;https://download.csdn.net/download/mingjing941018/20216747 2 Asp.net MVC将pdf转图片 使用Nuget安装包安装Freespire.pdf&#xff0c;控制器中相关代码&#xff1a; /// <summary>/// 将本地…

【基础知识】大数据组件HBase简述

HBase是一个开源的、面向列&#xff08;Column-Oriented&#xff09;、适合存储海量非结构化数据或半结构化数据的、具备高可靠性、高性能、可灵活扩展伸缩的、支持实时数据读写的分布式存储系统。 只是面向列&#xff0c;不是列式存储 mysql vs hbase vs clickhouse HMaster …

改变定时器获取传感器频度_广东梅州梅县压力传感器*校对

广东梅州梅县压力传感器*校对看门狗寄存器不会改变或改变不大&#xff0c;如果看门狗寄存器发生了改变或改变很大&#xff0c;则说明系统陷入“死循环”.需要进行出错处理。在工业应用中&#xff0c;严重的干扰有时会破坏中断方式控制字&#xff0c;关闭中断&#xff0c;造成看…

**【POJ - 2389】 Bull Math (高精度乘法)

题干&#xff1a; Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls answers. Read in two …

nodeType的类型

1&#xff1a;元素节点   2&#xff1a;属性节点   3&#xff1a;文本节点   4&#xff1a;CDATA区段   5&#xff1a;实体应用元素   6&#xff1a;实体   7&#xff1a;表示处理指令   8&#xff1a;注释节点   9&#xff1a;最外层的Root element,包括所有其…

springboot 不响应字段为空_面试官扎心一问:Tomcat 在 SpringBoot 中是如何启动的?...

作者&#xff1a;木木匠 http://my.oschina.net/luozhou/blog/3088908前言我们知道 SpringBoot 给我们带来了一个全新的开发体验&#xff0c;我们可以直接把 web 程序达成 jar 包&#xff0c;直接启动&#xff0c;这就得益于 SpringBoot 内置了容器&#xff0c;可以直接启动&am…

【POJ - 3250 】Bad Hair Day (单调栈)

题干&#xff1a; Some of Farmer Johns N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows heads. Each cow i has a s…

a1708硬盘转接口_资讯:希捷上架新款银河Exos系列机械硬盘,15000转+SAS协议

今日最新消息&#xff0c;希捷上架一款新品希捷银河Exos系列机械硬盘。据悉这款硬盘采用了SAS协议&#xff0c;转速高达15000RPM&#xff0c;目前公布的售价600GB为1899元RMB。据官方介绍这款希捷银河Exos系列机械硬盘为2.5英寸&#xff0c;15mm的厚度&#xff0c;最高的转速可…

ACM中关于计算几何(浮点数)的精度问题

计算几何的精度问题说到底其实是浮点数的精度问题&#xff0c;但我觉得“计算几何”比“浮点数”更能吸引眼球&#xff0c;所以选了这个标题。 1.浮点数为啥会有精度问题&#xff1a; 浮点数(以C/C为准)&#xff0c;一般用的较多的是float, double。 占字节数 数值范围 十进…

微信公众号网站开发相关记录

1 如何监听微信录音是否正常开启 wx.startRecord({success: function (ret) {alert("开始录音" JSON.stringify(ret));},fail: function (err) {alert("无法录音" JSON.stringify(err));}});

【POJ - 1182】 食物链(附超详细讲解)(并查集--种类并查集经典题)

题干&#xff1a; 动物王国中有三类动物A,B,C&#xff0c;这三类动物的食物链构成了有趣的环形。A吃B&#xff0c; B吃C&#xff0c;C吃A。 现有N个动物&#xff0c;以1&#xff0d;N编号。每个动物都是A,B,C中的一种&#xff0c;但是我们并不知道它到底是哪一种。 有人用两…

腐蚀单机怎么进_暖气片堵塞是什么原因?要怎么解决呢?

你知道散热器到底为什么堵塞吗&#xff1f;散热器堵塞了怎么办&#xff1f;下面和金旗舰散热器小编一起来看看吧~一、散热器堵塞怎么办首先&#xff0c;把进回水阀先全部关闭&#xff0c;用扳手将散热器的堵头轻轻拧开。这里需要注意的是&#xff0c;堵头对应的散热器下面要放一…

layui弹出界面空白页问题

弹出界面时&#xff0c;有时会出现空白界面&#xff0c;应该如何处理&#xff1f; 1 尝试解决方式&#xff1a;在open方法的success回调方法中&#xff0c;获取当前iframe高度&#xff0c;重新赋予新的高度&#xff1b; let ifr layero.find(iframe)[0]; let bHeight ifr.s…

vspy如何在图形面板显示报文_设备实时状态监控:如何进行工业生产设备数据采集?...

设备实时状态监控&#xff1a;如何进行工业生产设备数据采集&#xff1f;数据采集(DAQ)&#xff0c;是指从传感器和其它待测设备等模拟和数字被测单元中自动采集非电量或者电量信号,送到上位机中进行分析&#xff0c;处理。慧都设备数据采集系统解决方案工业生产设备数据采集是…

【POJ - 2236】Wireless Network (并查集)

题干&#xff1a; An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers …

如何使用微信公众平台测试号进行系统开发

申请一个测试号&#xff1a;入口修改测试公众号自定义菜单&#xff08;使用微信公众平台接口调试工具&#xff09;网站开发&#xff0c;进行部署网站测试

【POJ - 1751】Highways (最小生成树)

题干&#xff1a; The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the …

jupyter怎么安装jieba_AI工具:Anaconda中Jupyter不能import已安装module问题解决

jupyter模式下写代码时,通过pip install package命令行安装package完成之后,无法在jupyter模式下import &#xff0c;这是个通用的问题&#xff0c;我这里遇到的是import jieba&#xff0c;可能import 别的package也会出现&#xff0c;记录下&#xff0c;也花了点时间排查。。。…

Sql Server数据库设置一个账户只能看到一个数据库

1 新建登录名&#xff0c;注意不要设置用户映射&#xff0c;服务器角色只选择public&#xff08;默认必选&#xff0c;无法去掉&#xff0c;可以添加其他服务器角色&#xff0c;但是不要添加查看所有数据库的权限&#xff0c;接下来会去掉public的查看所有数据库权限&#xff0…

boot lib分离 spring_spring boot + gradle打包bootJar分离lib

以前项目打包一直是用的maven&#xff0c;最近新开一个项目&#xff0c;使用的是spring boot 2.11 gradle 4.10.3&#xff0c;在打包的时候分离lib折腾了好几天&#xff0c;网上找了很多方法都不成功&#xff0c;老是卡在configurations.compile这里&#xff0c;总是获取不到正…