poj 2559 Largest Rectangle(单调栈)
Largest Rectangle in a Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26549 | Accepted: 8581 |
Description
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:
Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.
Input
The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1<=n<=100000. Then follow n integers h1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.
Output
For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.
Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output
8
4000
单调栈入门题...
#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<algorithm>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
int a[100005];
int st[100005];
int L[100005];
int R[100005];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLIN
while(1)
{
scanf("%d",&n);
if(n==0)break;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
stack<int>st;
for(int i=1;i<=n;i++)
{
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(st.empty()){L[i]=1;st.push(i);continue;}
L[i]=st.top()+1;
st.push(i);
}
while(!st.empty())st.pop();
for(int i=n;i>=1;i--)
{
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(st.empty()){R[i]=n;st.push(i);continue;}
R[i]=st.top()-1;
st.push(i);
}
ll ans=0;
for(int i=1;i<=n;i++)
{
ans=max(ans,1ll*a[i]*(R[i]-L[i]+1));
}
printf("%lld\n",ans);
}
return 0;
}
poj 2559 Largest Rectangle(单调栈)的更多相关文章
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- 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(单调栈) && 单调栈
嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...
- 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(单调栈)
[题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...
随机推荐
- java 利用辗除法求两个整数的最大公约数和最小公倍数
题目:输入两个正整数m和n,求其最大公约数和最小公倍数. 程序分析:利用辗除法. package Studytest; import java.util.Scanner; public class P ...
- (4.16)mysql备份还原——物理备份之XtraBackup实践
关键词:XtraBackup实践,物理备份,xtrabackup备份,innobackupex备份 [1]如何使用? [3]系列:innobackupex --help |more [4]系列:xtr ...
- ORACLE数据库备份与恢复详解
ORACLE数据库备份与恢复详解 学习过程中的总结,有兴趣不妨看看,如果有不对的地方,高手不要留情!! Oracle的备份与恢复有三种标准的模式,大致分为两 大类,备份恢复(物理上的)以及导入导出(逻 ...
- GitHub从小白到熟悉<四>
GitHub issue 使用教程 创建 一个issue (显示所有bug 或者 说 交流的 问题列表)
- Adam作者大革新, 联合Hinton等人推出全新优化方法Lookahead
Adam作者大革新, 联合Hinton等人推出全新优化方法Lookahead 参与:思源.路.泽南 快来试试 Lookahead 最优化方法啊,调参少.收敛好.速度还快,大牛用了都说好. 最优化方 ...
- IntelliJ IDEA 复杂的重构技巧
IntelliJ IDEA 复杂的重构技巧(二) 转载 上次我说了一些 "复杂的重构技巧" ,讲的是一些使用 IntelliJ 的简单功能实现复杂的重构需求的技巧. 看到大家的反响 ...
- Keepalived+Nginx+Tomcat 实现高可用Web集群
https://www.jianshu.com/p/bc34f9101c5e Keepalived+Nginx+Tomcat 实现高可用Web集群 0.3912018.01.08 20:28:59字数 ...
- Linux系统定时备份网站文件到七牛云存储脚本
1.七牛云账号注册. 有些朋友可能会问为什么要备份到七牛云?很简单,七牛云免费注册就能获取1G的存储空间,只要简单几步操作完成实名认证即可获取10G的存储空间,对于一般个人站点来说已经足够备份使用了. ...
- O017、部署DevStack
参考https://www.cnblogs.com/CloudMan6/p/5357273.html 本节按照以下步骤部署 DevStack 实验环境,包括控制节点和计算节点.详细的部署和配置可以 ...
- AES加密解密 Java中运用
AES全称 Advanced Encryption Standard, 高级加密算法,更加安全,可取代DES. Aes: package com.blog.d201706.encrypt; impor ...