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的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...
随机推荐
- JavaSE 基础
一.Java 面向对象 1. 面向对象都有哪些特性以及 你对这些特性的理解 1.1. 继承: 继承是从已有类得到继承信息创建新类的过程. 提供继承信息的类被称为父类(超类,基类);得到继承信息的类被称 ...
- Codeforces 1196F. K-th Path
传送门 发现到 $K$ 不大,考虑有什么和 $K$ 有关的结论 发现答案似乎只会经过前 $K$ 小的边,如果边权第 $K$ 小的边有多条那么可以任意取 证明挺显然的吧,首先如果走了边权排名大于 $K$ ...
- JS基础_字面量和变量
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 怎么定义vue-router的动态路由?怎么获取传过来的动态参数?
在router目录下的index.js文件中,对path属性加上/:id. 使用router对象的params.id 例如 : this.$route.params.id
- iOS获取APP的版本号和名称
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // ap ...
- 【异常】Reason: Executor heartbeat timed out after 140927 ms
1 详细异常 ERROR scheduler.JobScheduler: Error running job streaming job ms. org.apache.spark.SparkExcep ...
- re:正则表达式,字符串处理的杀手锏
介绍 正则表达式是一种用形式化语法描述的文本匹配模式,可以进行复杂的字符串匹配. Python中的正则表达式通过re模块提供,功能比Python内置的str更强,但是速度没有str提供的方法快. 因此 ...
- 网络初级篇之网络设备的FTP(原理与实验)
一.什么是FTP FTP就是文件传输协议.用于互联网双向传输.二.FTP的作用 控制文件下载空间在服务器复制文件从本地计算机或本地上传文件复制到服务器上的空间,主要的作用就是文件的传输,保 ...
- 2019-2020-1 20199319《Linux内核原理与分析》第四周作业
MenuOS的构造 基础知识 1.操作系统的两把宝剑:①中断上下文的切换:保存现场和恢复现场:②进程上下文的切换. 2.Linux内核以A.B.C.D方式命名:A和B变得无关紧要,C是内核的真实版本, ...
- docker_facenet_image在Docker容器中运行Facenet环境搭建
对开发和运维人员来说,可能最梦寐以求的就是一次性地创建或配置,可以在任意环境.任意时间让应用正常运行.而Docker恰恰是可以实现这一终极目标的瑞士军刀. 具体来说,Docker在开发和运维过程中,具 ...