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 ...
随机推荐
- GFS, HDFS, Blob File System架构对比
分布式文件系统很多,包括GFS,HDFS,淘宝开源的TFS,Tencent用于相册存储的TFS (Tencent FS,为了便于区别,后续称为QFS),以及Facebook Haystack.其中,T ...
- TYVJ4623 球球大作战·生存
时间: 500ms / 空间: 65536KiB / Java类名: Main 背景 小天很喜欢玩球球大作战这个游戏,大家也应该都玩过.游戏规则是:移动自己的球,移动到别人的球(一定要比自己的球小)的 ...
- dos中定义变量与获取常见的引用变量以及四则运算、备份文件(set用法)
在dos中使用set定义变量: set a=8 (注意等号两边没有空格) 引用变量如: echo %a% 将打印a的值 (%a%是获取变量a的值) dos中 ...
- Codevs1062路由选择
/* #include<iostream> #include<cstdio> #include<cstring> #define MAXN 301 using na ...
- 前端微服务-面向web平台级应用的设计
从去年开始,前端领域就出现了一个‘微应用’的名词,说的是前端架构的一种设计思路,业内都把它和后端的微服务进行类比,当时忙于公司的项目.没有静下心来好好了解,现在项目结束,再加上最近看的几篇关于前端微服 ...
- Service具体解释(一):什么是Service
< Service具体解释(一):什么是Service> < Service具体解释(二):Service生命周期> <Service具体解释(三):Service的使用 ...
- 静态NAT、动态NAT、PAT(端口多路复用)的配置
静态NAT.动态NAT.PAT(端口多路复用)的配置 NAT的实现方式有三种,即静态转换Static Nat.动态转换Dynamic Nat 和 端口多路复用OverLoad. 静态转换 ( ...
- Seven times have I despised my soul 《我曾七次鄙视自己的灵魂》
<我曾七次鄙视自己的灵魂>,纪伯以“自己的灵魂”为名,看穿人性所共有弱点的一首诗.诗句简单有力发人深省,督促人们拥有更高的精神境界,呼吁人们涤荡自己的灵魂,唾弃丑恶,追求高尚. Seven ...
- 怎样去除JSP页面提示:Cannot return from outside a function or method.
今天用myeclipse10写JSP页面时出现: Cannot return from outside a function or method. onClick="return ch ...
- node 爬虫 --- 批量下载图片
步骤一:创建项目 npm init 步骤二:安装 request,cheerio,async 三个模块 request 用于请求地址和快速下载图片流. https://github.com/reque ...