// poj 2559 Largest Rectangle in a Histogram 栈
//
// n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起)
//
// 这道题用的是数据结构做。也能够递推做。眼下仅仅会数据结构的
//
// 对于每一个高度h,求一个左边界L和右边界R,分别表示的意义是
// L是下标为j的矩形的高度的hj小于当前h的最大的j的值。 则依据定义
// 我们能够知道j到i之间的h都是大于当前的hi的。
// R是下标为k的矩形的高度的hk大于当前h的最小的k的值。则依据定义
// 我们能够知道i到k之间的h都是大于当前的hi的。
// 则最后的结果就是max( ( R[i] - L[i] ) * h[i]).
//
// 详细实现是用一个栈依次保存每一个矩形的高度。
// 设栈中的元素从上到下的值是x[i](x[i] > x[i+1] && h[x[i]] > h[x[i+1]])
//
// 在计算L[i]的时候。当栈顶元素j满足h[j]>=h[i]时。一直出栈。直到j=0或者
// h[j] < h[i] 的时候,我们就求出了最大的h[j]>=h[i]的j的最小值即j+1
//
// 在计算R[i]的时候,当栈顶元素j满足h[j]>=h[i]时,一直出栈。知道j=0或者
// h[j] < h[i] 的时候。我们就求除了最小的h[j]>=h[i]的j的最大值即j。
//
// 所要注意的是
//
// 计算L的时候要从左边開始扫描,此时栈中须要的是1,2,...i的值
// 计算R的时候要从右边開始扫描,此时栈中须要的是i+1...n的值
//
//
// 感悟:
//
// 从这道题中就能够发现数据结构栈的魅力的所在,个人感觉数据结构非常奇妙,
// 也更加笃定了我要学数据结构的决心。 //
// 继续练 #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ceil(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define gcd __gcd
#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
#define popCount __builtin_popcountll
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const long double PI = acos(-1.L); const int maxn = 1e5 + 8;
int a[maxn];
int st[maxn];
int n;
int L[maxn];
int R[maxn];
void init(){
for (int i=0;i<n;i++)
scanf("%d",&a[i]);
int t = 0; for (int i=0;i<n;i++){
while(t>0 && a[st[t-1]]>=a[i]) t--;
L[i] = t==0 ? 0 : st[t-1] + 1;
st[t++] = i;
}
t = 0;
for (int i=n-1;i>=0;i--){
while(t>0 && a[st[t-1]] >= a[i]) t--;
R[i] = t==0 ? n : st[t-1];
st[t++] = i;
}
long long res = 0;
for (int i=0;i<n;i++){
res = max(res,( R[i] - L[i] ) * (long long)a[i]);
}
printf("%lld\n",res);
} int main() {
//freopen("G:\\Code\\1.txt","r",stdin);
while(scanf("%d",&n)!=EOF&&n){
init();
}
return 0;
}

poj 2559 Largest Rectangle in a Histogram 栈的更多相关文章

  1. [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

    [POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...

  2. stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

    题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...

  3. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  4. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

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

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  6. POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831 ...

  7. 题解报告: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 ...

  8. 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 ...

  9. POJ 2559 Largest Rectangle in a Histogram

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18942   Accepted: 6083 Description A hi ...

随机推荐

  1. SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集

    [题目分析] 区间开方+区间求和. 由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可. 这不是BZOJ上上帝造题7分钟嘛 [代码] #include < ...

  2. 算法复习——迭代加深搜索(骑士精神bzoj1085)

    题目: Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相 ...

  3. P1133 教主的花园 (动态规划)

    题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢 3种树,这3种 ...

  4. Contest Hunter #46 T1 磁力块 [分块]

    描述 在一片广袤无垠的原野上,散落着N块磁石.每个磁石的性质可以用一个五元组(x,y,m,p,r)描述,其中x,y表示其坐标,m是磁石的质量,p是磁力,r是吸引半径.若磁石A与磁石B的距离不大于磁石A ...

  5. Codevs 2602 最短路径问题

     时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间. ...

  6. Spring Batch 文档(中文)

    http://blog.csdn.net/shorn/article/category/1186181

  7. T1164 统计数字 codevs

    http://codevs.cn/problem/1164/ 题目描述 Description [问题描述]某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109).已知不 ...

  8. java list实现树形结构

    1.javabean import java.util.List; public class TreeNode { private String id; private String parentId ...

  9. nuxt.js 加百度统计

    Mark一下: 在 Nuxt.js应用中使用Google统计分析服务,或者百度统计分析服务,推荐在 plugins 目录下创建 plugins/ga.js 文件.统计统计分析我们可以获取网站pv,uv ...

  10. 一步步走向国际乱码大赛-- 恶搞C语言

    大家都一直强调规范编码.可是这个世界上有个大师们娱乐的竞赛--国际乱码大赛. 能写出来的都是对语言深入了解的master.我从没想自己也能"恶搞"C,一直都是老老实实编码.就在前几 ...