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 ...
随机推荐
- 【bzoj3231】[Sdoi2008]递归数列 矩阵乘法+快速幂
题目描述 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 + ... + ckai-k 其中bj和 cj ...
- W3 School学习网站
http://www.w3school.com.cn/ 领先的 Web 技术教程 - 全部免费 在 w3school,你可以找到你所需要的所有的网站建设教程. 从基础的 HTML 到 CSS,乃至进阶 ...
- jenkins执行自动化用例(详细、有用、mark 优先级高高高)
http://blog.sina.com.cn/s/blog_68f262210102vx8o.html 第七章 测试用例接入jenkins自动运行 ------Web自动化测试之Webdriver+ ...
- uva 11997 优先队列
K Smallest Sums You're given k arrays, each array has k integers. There are kk ways to pick exactly ...
- Cucumber Vs RobotFramework
I asked this same question a little over a year ago on this list when we were at your stage. Before ...
- Redis命令行之List
一.Redis之List简介 1. List是简单的字符串列表,按照插入顺序排列. 2. 一个列表最多可存储232-1个元素(40多亿). 二.Redis之List命令行操作 Lrange:获取列表指 ...
- (7)C#连DB2---oledb方式
1安装客户端 安装DbVisualizer Free 客户端软件 2编目 用 win+r 输入 db2cmd 启动命令行 要远程操作数据库,首先要进行编目,分三个步骤: 1. 在客户端建立服务器端数 ...
- 《Java虚拟机原理图解》 1.2、class文件中的常量池
了解JVM虚拟机原理 是每一个Java程序员修炼的必经之路.但是由于JVM虚拟机中有很多的东西讲述的比较宽泛,在当前接触到的关于JVM虚拟机原理的教程或者博客中,绝大部分都是充斥的文字性的描述,很难给 ...
- axis2调用WSDL接口
public static JSONObject sendWsdl(String url,String xmlStr){ JSONObject res=new JSONObject(); try { ...
- iOS Application Security
文章分A,B,C,D 4个部分. A) iOS Application Security 下面介绍iOS应用安全,如何分析和动态修改app. 1)iOS Application security Pa ...