poj 2559 Largest Rectangle in a Histogram 栈
// 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 栈的更多相关文章
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram
题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...
- 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 - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15831 ...
- 题解报告: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 Description A histogram is a polygon composed of a sequence of r ...
- POJ 2559 Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18942 Accepted: 6083 Description A hi ...
随机推荐
- Codeforces 547B. Mike and Feet[单调栈/队列]
这道题用单调递增的单调栈维护每个数能够覆盖的最大区间即可. 对于 1 2 3 4 5 4 3 2 1 6 这组样例, 1能够覆盖的最大区间是10,2能够覆盖的最大区间是7,以此类推,我们可以使用单 ...
- 【Luogu】P2158仪仗队(欧拉函数)
题目链接 首先来介绍欧拉函数. 设欧拉函数为f(n),则f(n)=1~n中与n互质的数的个数. 欧拉函数有三条引论: 1.若n为素数,则f(n)=n-1; 2.若n为pa,则f(n)=(p-1)*(p ...
- bzoj4002 [JLOI2015]有意义的字符串 快速幂
Description B 君有两个好朋友,他们叫宁宁和冉冉. 有一天,冉冉遇到了一个有趣的题目:输入 b;d;n,求((b+sqrt(D)/2)^N的整数部分,请输出结果 Mod 752844341 ...
- android获取手机号
private String getPhoneNum(){ //与手机建立连接 TelephonyManager tm = (TelephonyManager)getSystemService(Con ...
- poj - 2186 Popular Cows && poj - 2553 The Bottom of a Graph (强连通)
http://poj.org/problem?id=2186 给定n头牛,m个关系,每个关系a,b表示a认为b是受欢迎的,但是不代表b认为a是受欢迎的,关系之间还有传递性,假如a->b,b-&g ...
- T3137 栈练习1 codevs
codevs.cn/problem/3137 题目描述 Description 给定一个栈(初始为空,元素类型为整数,且小于等于100),只有两个操作:入栈和出栈.先给出这些操作,请输出最终栈的栈顶元 ...
- T1079 回家 codevs
http://codevs.cn/problem/1079/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver~死坑 题目描述 Description 现在是晚 ...
- nginx原配置
#原配置 server { listen ; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main ...
- 《深入理解mybatis原理》 Mybatis初始化机制详解
对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外.本章将通过以下几点详细介绍MyBatis的初始化过程. 1.MyBatis的初始化做了什么 2. MyBatis基于XML配置 ...
- webstorm bable
一.设置npm源 1.得到原本的镜像地址 npm get registry > https://registry.npmjs.org/ 设成淘宝的 npm config set registry ...