Largest Rectangle in a Histogram-POJ2559
来源:互联网 发布:启动oracle数据库命令 编辑:程序博客网 时间:2024/06/03 15:11
原题链接http://poj.org/problem?id=2559
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 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 34 1000 1000 1000 10000
Sample Output
84000
tips:
单调栈
思路:
index012345678height021451330栈内容(上面栈底)(0,0)(0,0)(2,1)(0,0)
(1,1)(0,0)
(1,1)
(4,3)(0,0)
(1,1)
(4,3)
(5,4)(0,0)
(1,1)
(1,3)(0,0)
(1,1)
(1,3)
(3,6)(0,0)
(1,1)
(1,3)
(3,6)
(3,7)
代码:
#include <stdio.h>#include <stack>using namespace std;struct SNode{long long height;int startIdx;SNode(long long h, int idx):height(h), startIdx(idx){}};long long heightArr[100000];long long getMaxArea(int num){long long maxArea = 0;stack<SNode> stk;stk.push(SNode(0, 0));for(int i = 0; i <= num; i++){long long curh = 0;if(i < num){curh = heightArr[i];}int curStartIdx = i + 1;while(curh < stk.top().height){SNode t = stk.top();curStartIdx = t.startIdx;long long curArea = t.height * (i + 1 - t.startIdx);if(curArea > maxArea) maxArea = curArea;stk.pop();}stk.push(SNode(curh, curStartIdx));}return maxArea;}int main(){int num;while(scanf("%d", &num) != EOF && num){for(int i=0; i< num; i++){scanf("%lld", heightArr + i);}printf("%lld\n", getMaxArea(num));}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(单调栈)
- Genymotion启动失败的解决方法
- 设计模式原则概述
- 【心情】"支NMLGB配树”
- java对象值传递和对象传递的总结
- Debian7 U盘安装过程和小结
- Largest Rectangle in a Histogram-POJ2559
- 字符串算法题目
- surfaceflinger workflow
- 读书笔记:"算法导论"之RANDOMIZED-SELECT(快速选择算法)
- 关于找工作中Java问题
- 【排序】直接插入排序
- 单片机编程经验总结
- Binary Tree(构造)
- Android 4.0的图形硬件加速及绘制技巧