题解 POJ 2559-SP1805 【HISTOGRA - Largest Rectangle in a Histogram】
题目链接: https://www.luogu.org/problemnew/show/SP1805
http://poj.org/problem?id=2559思路:
单调栈
首先如果所有矩形的高度是单调递增的,即新加入的矩形比上一个高,那么把加进来直至加完,这是我们就把分别每个矩形的高度看做是最大子矩形的高度,分别向左右拓展求得最大面积。
然而矩形的高度不一定是递增的,如果新加入的矩形比上一个小,那么之前这些矩形的高度对于后面的计算就没用了。我们就在这时候把比这个新加的矩形高的部分全部删掉,这样就能保持矩形都是单调递增的。
具体实现的方法简单说就是将之前比这高的矩形弹出栈知道遇到比新矩形低的矩形,同时累计他们的宽度之和,乘以这个更矮矩形的高度来更新答案。最后再按这个方法把所有矩形弹出去来更新答案。
小技巧:
- 将a[n+1]设为0保证最后能将所有矩形弹出去(想想为什么)
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cctype>
#define ll long long
using namespace std;
const int maxn=100010;
int a[maxn],w[maxn],s[maxn];//w记录宽度
ll ans;
int n;
template <class T>inline void read(T &x){
x=0;char c;int neg=0;
while(!isdigit(c=getchar()))neg=c=='-';
x=c-48;
while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48;
x=neg?-x:x;
return ;
}
inline void solve(){
int p;
a[n+1]=p=0;//这样最后能将所有矩形弹出栈
for(register int i=1;i<=n+1;i++){
if(a[i]>s[p]){
s[++p]=a[i];
w[p]=1;
}
else {
int wid=0;
while(s[p]>a[i]){
wid+=w[p];
ans=max(ans,(ll)wid*s[p]);
p--;
}
s[++p]=a[i],w[p]=wid+1;
}
}
cout<<ans<<endl;
return;
}
int main()
{
while(scanf("%d",&n)!=EOF){
if(!n)break;
ans=0;
for(register int i=1;i<=n;i++){
read(a[i]);
}
solve();
}
return 0;
}
题解 POJ 2559-SP1805 【HISTOGRA - Largest Rectangle in a Histogram】的更多相关文章
- SP1805 HISTOGRA - Largest Rectangle in a Histogram 题解
题目链接:https://www.luogu.org/problemnew/show/SP1805 分析: 我们可以用一个单调栈由低到高来存储它的高度,并用数组对每个高度记录一下它前面(包括它自己)一 ...
- SP1805 HISTOGRA - Largest Rectangle in a Histogram
--------------------------------------------------- 我就是想学个单调栈然后全网都是个蓝题 ----------------------------- ...
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
- poj 2559 Largest Rectangle in a Histogram (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- 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 ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram
题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...
- HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram
http://acm.hdu.edu.cn/showproblem.php?pid=1506 || http://poj.org/problem?id=2559 Time Limit: 2000/1 ...
随机推荐
- el-select获取选中的value和label
selectGetFn(val) { if (this.multipleFlag) { for (let i = 0; i < GUIDArr.length; i++) { let obj = ...
- [.NET逆向] [入门级]de4dot参数详解
为了避免被0xd4d(de4dot作者)认为是"N00bUser"为了认识到Some of the advanced options may be incompatible, ca ...
- APP测试要点整理
APP测试基本流程以及APP测试要点https://www.cnblogs.com/dengqing9393/p/6497068.html 性能测试:https://blog.csdn.net/xia ...
- 防止同一IP多次请求攻击
防止同一IP多次请求攻击 防止入侵者,通过死循环同一时间批量向服务器请求数据,导致服务器内存开销不断膨胀,最后直接瘫痪. 一. 新增一个spring的拦截器 , 拦截所有请求 <mvc:inte ...
- Filebeat的使用
前言 logstash本身就可以具有文件数据采集的功能了,为什么还需要在前面加一层filebeat?理由如下:logstash是使用Java编写,插件是使用JRuby编写,对机器的资源要求会比较高,在 ...
- Python向excel中写入数据的方法 方法简单
最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中. 数据导入之前需要安装 xlwt依赖包,安装的方法就很简单,直接 p ...
- Nginx添加静态页面
1.编辑配置文件 sudo vim /etc/nginx/nginx.conf 在http {}中添加如下 server { listen 80; server_name localhost; loc ...
- Transaction-Mybatis源码
github地址:https://github.com/dchack/Mybatis-source-code-learn (欢迎star) TransactionFactory 官方文档: 在 MyB ...
- 【ML基础】t-SNE(t-distributed stochastic neighbor embedding)原理及推导
前言 参考 1. t-SNE原理与推导: 完
- javascript Round Function
var rounded = Math.round( number * 10 ) / 10; // round to one digit var rounded = Math.round( number ...