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 ...
随机推荐
- C Looooops(poj 2115)
大致题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. 解题思路: 题意不难理解,只是利用 ...
- AnyChart创建实时仪表
AnyChart创建实时仪表 简述: AnyChart是一款基于Flash和HTML5的图表.仪表控件,所包含的图表类型众多和跨平台以及跨浏览器是该产品的主要特点和优点,另外该产品基于XML文件作为数 ...
- springboot收集
Spring Boot实战:拦截器与过滤器 参考:https://blog.csdn.net/m0_37106742/article/details/64438892 https://www.ibm. ...
- MySQL-JDBC Loadbalance深入解析
背景说明 公司的整个电商系统搭建在华为云上,根据老总的估计,上线3个月之后日订单量会达到百万级别,保守估计3个月之后总订单个数预计会有5千万.MySQL单表达到千万级别,就会出现明显的性能问题.根据如 ...
- Xen虚拟化
Xen虚拟化基础 Xen虚拟化类型 hypervisor Xen组件 Xen hypervisor Colletion CPU.Memory.Interrupter Domain0 ---> D ...
- python--输出自己需要的字符串连接的的方式
python中有很多字符串连接方式,今天在写代码,顺便总结一下,从最原始的字符串连接方式到字符串列表连接,大家感受下: 最原始的字符串连接方式:str1 + str2 python 新字符串连接语法: ...
- (44)C#网络2
一.用SmtpClient类发送邮件 允许应用程序使用简单邮件传输协议 (SMTP) 发送电子邮件 using System.Net.Mail; SmtpClient smtpClient = new ...
- codevs——T1860 最大数||洛谷——P1107 最大整数
http://codevs.cn/problem/1860/ || https://www.luogu.org/problem/show?pid=1107#sub 题目描述 Description 设 ...
- 对jquery插件Jcrop开发一个裁剪组件
Jcrop是一款优秀的裁剪工具,它不仅可以裁剪图像,还可以裁剪canvas及任何的div元素,具体可参考: http://code.ciaoca.com/jquery/jcrop/ 基于Jcrop,开 ...
- 前端MVC Vue2学习总结(九)——Vuex状态管理插件
一.概要 1.1.Vuex定义与注意事项 Vuex是为vue.js框架更好的管理状态而设计一个插件.Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的 ...