POJ2559
来源:互联网 发布:jdk源码有哪些值得买 编辑:程序博客网 时间:2024/06/11 21:50
1.题目描述:
Description
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
Output
Sample Input
7 2 1 4 5 1 3 34 1000 1000 1000 10000
Sample Output
84000
Hint
Source
给定从左到右多个矩形,已知这此矩形的宽度都为1,长度不完全相等。这些矩形相连排成一排,求在这些矩形包括的范围内能得到的面积最大的矩形,打印出该面积。所求矩形可以横跨多个矩形,但不能超出原有矩形所确定的范围。
3.解题思路:
建立一个单调递增栈,所有元素各进栈和出栈一次即可。每个元素出栈的时候更新最大的矩形面积。
设栈内的元素为一个二元组(x, y),x表示矩形的高度,y表示矩形的宽度。
若原始矩形高度分别为2,1,4,5,1,3,3
高度为2的元素进栈,当前栈为(2,1)
高度为1的元素准备进栈,但必须从栈顶开始删除高度大于或等于1的矩形,因为2已经不可能延续到当前矩形。删除(2,1)这个元素之后,更新最大矩形面积为2*1=2,然后把它的宽度1累加到当前高度为1的准备进栈的矩形,然后进栈,当前栈为(1,2)
高度为4的元素进栈,当前栈为(1,2) (4,1)
高度为5的元素进栈,当前栈为(1,2) (4,1) (5,1)
高度为1的元素准备进栈,删除(5,1)这个元素,更新最大矩形面积为5*1=5,把1累加到下一个元素,得到(4,2),删除(4,2),更新最大矩形面积为4*2=8,把2累加到下一个元素,得到(1,4),1*4=4<8,不必更新,删除(1,4),把4累加到当前准备进栈的元素然后进栈,当前栈为(1,5)
高度为3的元素进栈,当前栈为(1,5) (3,1)
高度为3的元素准备进栈,删除(3,1),不必更新,把1累加到当前准备进栈的元素然后进栈,当前栈为(1,5) (3,2)
把余下的元素逐个出栈,(3,2)出栈,不必更新,把2累加到下一个元素,当前栈为(1,7),(1,7)出栈,不必更新。栈空,结束。
最后的答案就是8。
4.AC代码:
#include <stdio.h>#include <algorithm>#define maxn 100100using namespace std;typedef long long ll;ll dpl[maxn], dpr[maxn], a[maxn];int main(){ll n;while (scanf("%lld", &n) != EOF && n){for (ll i = 1; i <= n; i++)scanf("%lld", &a[i]);dpl[1] = 1;dpr[n] = n;for (ll i = 2; i <= n; i++)//左边和他一样小的{ll flag = i;while (flag > 1 && a[flag - 1] >= a[i])flag = dpl[flag - 1];dpl[i] = flag;}for (ll i = n - 1; i >= 1; i--){ll flag = i;while (flag < n && a[flag + 1] >= a[i])flag = dpr[flag + 1];dpr[i] = flag;}ll ans = 0;for (ll i = 1; i <= n; i++)ans = max(ans, (dpr[i] - dpl[i] + 1) * a[i]);printf("%lld\n", ans);} return 0;}
- poj2559
- poj2559
- POJ2559
- POJ2559.md
- POJ2559 最大矩形面积
- 栈的运用-poj2559
- poj2559模拟单调栈
- poj2559栈应用
- poj2559单调栈
- POJ2559 单调队列
- POJ2559 最大矩形面积
- poj2559(单调栈)
- POJ2559单调栈
- POJ2559 Largest Rectangle in a Histogram(DP)
- poj2559 我要做的足够 强大
- 萌萌的单调栈 : poj2796 , poj2559 ,
- poj2559 Largest Rectangle in a Histogram
- poj2559 Largest Rectangle in a Histogram
- [AOP] 4. Spring AOP中提供的种种Aspects
- 怎么在自定义函数里更改主函数的数据取值
- ACM程序设计 书中题目Y
- 授权
- VR小项目(二)
- POJ2559
- css盒子模型
- 路由选择协议
- js、jquery遍历数组和对象
- 欢迎使用CSDN-markdown编辑器
- 论菜鸡的一生 之心态爆炸,在下实在缺少勇气
- 小白转载~关于javaEE的MVC(三大层)简单描述(摘抄)
- centos6.5配置网卡:动态和静态配置
- 剑指OFFER-二维数组中的查找