POJ2559 Largest Rectangle in a Histogram
来源:互联网 发布:星空直播网络电视apk 编辑:程序博客网 时间:2024/04/29 03:38
Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 21267 Accepted: 6851
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.
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 integersh1,...,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 34 1000 1000 1000 10000
Sample Output
84000
Hint
Huge input, scanf is recommended.
当时刚做完bc,学了单调栈和单调队列,找了点题目做做,这是个单调栈的题目
题目要求求出这个条形图的最大阴影面积且要求是矩形,暴力解决过,没成功。用单调栈做,这里是单调递增栈。单调递增栈的概念就是只有当一个数比栈顶的元素大的时候才入栈,若比栈顶元素小的时候则对栈顶元素进行操作运算,并出栈直到栈顶元素小于当前数时再将当前的数入栈。单调递减栈则相反。
这个题目先从前向后遍历数组入栈计算出每个矩形(包括其本身)以自身为高的右半部分的最大面积,然后再从后向前遍历数组入栈计算出每个 矩形(包括其本身)以自身为高的左半部分的最大面积,那么第i个矩形左右两端的面积之和再减去其本身的面积(计算了两次),最后比较取最大值即可
#include <iostream>#include <cstdio>#include <cstring>#include <stack>using namespace std;typedef long long int ll;ll a[100005];ll l[100005];ll r[100005];struct node{ ll y; int x;};int main(){ int n; while(~scanf("%d",&n)&&n){ for(int i = 0;i < n;scanf("%I64d",&a[i++])); ll ans = 0; stack<node>s; memset(l,0,sizeof(l)); memset(r,0,sizeof(r)); node temp; temp.y = a[0],temp.x = 0; s.push(temp); int width; for(int i = 1;i < n;i++){ width = s.top().x; while(!s.empty()&&s.top().y>a[i]){ if(s.top().y*(width-s.top().x+1)>ans) r[s.top().x] = s.top().y*(width-s.top().x+1); s.pop(); } temp.y = a[i],temp.x = i; s.push(temp); } width = s.top().x; while(!s.empty()){ if(s.top().y*(width-s.top().x+1)>ans) r[s.top().x] = s.top().y*(width-s.top().x+1); s.pop(); } temp.x = n-1,temp.y = a[n-1]; while(!s.empty())s.pop(); s.push(temp); for(int i = n-2;i >= 0;i--){ width = s.top().x; while(!s.empty()&&s.top().y>a[i]){ if(s.top().y*(s.top().x-width+1)>ans) l[s.top().x] = s.top().y*(s.top().x-width+1); s.pop(); } temp.y = a[i],temp.x = i; s.push(temp); } width = s.top().x; while(!s.empty()){ if(s.top().y*(s.top().x-width+1)>ans) l[s.top().x] = s.top().y*(s.top().x-width+1); s.pop(); } for(int i = 0;i < n;i++) ans = max(ans,r[i]+l[i]-a[i]); printf("%I64d\n",ans); } return 0;}
0 0
- POJ2559 Largest Rectangle in a Histogram(DP)
- poj2559 Largest Rectangle in a Histogram
- poj2559 Largest Rectangle in a Histogram
- 【POJ2559】Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram-POJ2559
- POJ2559 Largest Rectangle in a Histogram
- poj2559 Largest Rectangle in a Histogram
- POJ2559-Largest Rectangle in a Histogram
- poj2559 Largest Rectangle in a Histogram
- POJ2559 Largest Rectangle in a Histogram [单调栈]
- poj2559 Largest Rectangle in a Histogram hdu1505 City Game
- poj2559 & zoj1985 &hdu1506 Largest Rectangle in a Histogram(笛卡尔树)
- POJ2559 Largest Rectangle in a Histogram(单调栈)
- POJ2559--Largest Rectangle in a Histogram(栈)
- 单调栈 poj2559 Largest Rectangle in a Histogram
- [POJ2559]Largest Rectangle in a Histogram(单调栈)
- POJ2559 Largest Rectangle in a Histogram(单调栈)
- poj2559 Largest Rectangle in a Histogram(单调栈)
- JavaScript之Prototype学习(一)
- CSS3 遇到的问题
- Android入门之搭建开发环境
- JAVA垃圾回收机制分析
- PAT(Advanced Level)1103. Integer Factorization
- POJ2559 Largest Rectangle in a Histogram
- NOI 2011 阿狸的打字机 AC自动机
- cocos2d-x 基础系列 (12) ParticleFireworks ParallaxNode示例
- 算法训练 瓷砖铺放
- [github项目]迅雷吸血进程自动清扫工具thunderAutoManager
- Ubuntu vi 上下左右变ABCD问题解决方法
- eclipse部署web项目至本地的tomcat但在webapps中找不到问题
- sdut 3867
- I apologize ( for sth )