单调栈--poj2559 Largest rectangle in a Histogram
2017-07-26 10:19
435 查看
一个直方图里,求最大的矩形。类似2 1 4 5 1 3 3
做完poj2796,再回来看,其实就是区间最小值乘区间长度吧。
解释见 http://blog.csdn.net/alongela/article/details/8230739
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespacestd;
const int maxn =100000 +5;
int h[maxn],dep[maxn];
typedef pair<int,int> pr;//记录矩形的高度,宽度
int main()
{
int n;
while(scanf("%d",&n) !=EOF
&& n)
{
memset(h,0,sizeof(h));
memset(dep,0,sizeof(dep));
for (int i =1; i <= n; i ++) {
scanf("%d",&h[i]);//1e9
}
stack<pr> stk;
longlong area =0;
int t =0;
for (int i =1; i <= n; i ++) {
while (!stk.empty() && stk.top().first
>= h[i]) {
area = max(area,(longlong)stk.top().first
* (stk.top().second + t));
t += stk.top().second;
stk.pop();
}
stk.push({h[i],t +1});
t = 0;
}
t = 0;
while (!stk.empty()) {
area = max(area,(longlong)stk.top().first
* (stk.top().second + t));
t += stk.top().second;
stk.pop();
}
printf("%lld\n",area);
}
return0;
}
做完poj2796,再回来看,其实就是区间最小值乘区间长度吧。
解释见 http://blog.csdn.net/alongela/article/details/8230739
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespacestd;
const int maxn =100000 +5;
int h[maxn],dep[maxn];
typedef pair<int,int> pr;//记录矩形的高度,宽度
int main()
{
int n;
while(scanf("%d",&n) !=EOF
&& n)
{
memset(h,0,sizeof(h));
memset(dep,0,sizeof(dep));
for (int i =1; i <= n; i ++) {
scanf("%d",&h[i]);//1e9
}
stack<pr> stk;
longlong area =0;
int t =0;
for (int i =1; i <= n; i ++) {
while (!stk.empty() && stk.top().first
>= h[i]) {
area = max(area,(longlong)stk.top().first
* (stk.top().second + t));
t += stk.top().second;
stk.pop();
}
stk.push({h[i],t +1});
t = 0;
}
t = 0;
while (!stk.empty()) {
area = max(area,(longlong)stk.top().first
* (stk.top().second + t));
t += stk.top().second;
stk.pop();
}
printf("%lld\n",area);
}
return0;
}
相关文章推荐
- POJ 2559 Largest Rectangle in a Histogram 单调栈
- POJ-2559-Largest Rectangle in a Histogram-单调栈
- HDU 1506 && POJ 2559 Largest Rectangle in a Histogram (单调队列)
- POJ 2559 Largest Rectangle in a Histogram RMQ || 单调栈
- Largest Rectangle in a Histogram POJ - 2559 单调栈
- 单调队列 POJ 2559 Largest Rectangle in a Histogram
- POJ2559 Largest Rectangle in a Histogram —— 单调栈
- POJ 2559 Largest Rectangle in a Histogram 单调栈
- [POJ 2559 Largest Rectangle in a Histogram] 单调栈
- 单调栈1002 POJ 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram --维护单调栈
- [poj 2559] Largest Rectangle in a Histogram:单调栈
- POJ - 2559 Largest Rectangle in a Histogram 单调栈
- 每日一dp(1)——Largest Rectangle in a Histogram(poj 2559)使用单调队列优化
- NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!
- 解题报告 之 POJ 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram (单调栈)
- Largest Rectangle in a Histogram(POJ-2559)
- poj 2559 Largest Rectangle in a Histogram