Largest Rectangle in a Histogram
2015-11-09 19:18
225 查看
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
这道题采用栈将递增的高度保存下来就行,碰到递减就将前面的面积全部算出来就可以,
不过我开始一直将x搞错了,x应该是当前坐标,保存x坐标时只需要保存前面比当前高度
低的x坐标。
NYOJ 258 用系统栈需要注意,保存出栈前一个的下标值。
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
这道题采用栈将递增的高度保存下来就行,碰到递减就将前面的面积全部算出来就可以,
不过我开始一直将x搞错了,x应该是当前坐标,保存x坐标时只需要保存前面比当前高度
低的x坐标。
#include <stdio.h> #define LL long long const int maxn = 100005; LL x[maxn], y[maxn], a[maxn]; inline LL Max ( LL a, LL b ) { return a > b ? a : b; } int main ( ) { int n, top; LL ans, t; while ( ~ scanf ( "%d", &n ) && n ) { top = -1; //初始化 for ( int i = 0; i < n; i ++ ) scanf ( "%lld", &a[i] ); a = -1; //最后会有没出栈的,添加一个-1将后面的全部出栈 top ++; ans = x[top] = 0; //ans初始,x y保存 y[top] = a[0]; for ( int i = 1; i <= n; i ++ ) { if ( a[i] > y[top] ) //递增就直接入栈 { top ++; x[top] = i; y[top] = a[i]; } else { t = i; //保存比a[i]小的x坐标 while ( top >= 0 && y[top] > a[i] ) { t = x[top]; //保存x坐标 ans = Max ( ans, ( i-x[top] )*y[top] ); //**这里要用i,x中间可能只保存了高度比他小的x坐标 top --; } //printf ( "%d %lld\n", i, t ); top ++; //入栈 x[top] = t; //保存最前面的x坐标 y[top] = a[i]; //高度 } } printf ( "%lld\n", ans ); } return 0; }
NYOJ 258 用系统栈需要注意,保存出栈前一个的下标值。
#include <stdio.h> #include <stack> #define LL long long using namespace std; const int maxn = 100005; LL a[maxn]; struct node { int x; LL y; node ( ) { } node ( int a, LL b ) { x = a; y = b; } }t; inline LL Max ( LL a, LL b ) { return a > b ? a : b; } int main ( ) { int n; stack < node > s; while ( ~ scanf ( "%d", &n ) && n ) { while ( ! s.empty ( ) ) s.pop ( ); for ( int i = 1; i <= n; i ++ ) scanf ( "%lld", &a[i] ); LL ans = 0; for ( int i = 1; i <= n; i ++ ) { if ( ! s.empty ( ) ) t = s.top ( ); if ( s.empty ( ) || a[i] >= t.y ) s.push ( node ( i, a[i] ) ); else { int pos = i; while ( t.y > a[i] ) { ans = Max ( ans, ( i-t.x )*t.y ); pos = t.x; //保存前一个下标 s.pop ( ); if ( s.empty ( ) ) break ; t = s.top ( ); } s.push ( node ( pos, a[i] ) ); //把那个下标加进去 } } while ( ! s.empty ( ) ) { t = s.top ( ); s.pop ( ); ans = Max ( ans, ( n+1-t.x )*t.y ); } printf ( "%lld\n", ans ); } return 0; }
相关文章推荐
- muduo库阅读(5)——线程
- OC -分数
- WebRTC VideoEngine 本地Video数据处理-VideoCaptureInputTest
- Eclipse+tomcat+MySQL搭建JavaWeb开发环境
- windows api 关机代码
- 海康SDK修改分辨率
- C++实现DPM/LatentSVM 完整代码下载 --- 第三篇
- SVN安装配置与使用
- android 自定义 环形进度条
- UI设计工具
- MVC 百度地图的基本使用
- 【转】jmeter 进行java request测试
- 块状元素和内联元素
- 计算机类职业资格划分
- Leetcode147: Flatten Binary Tree to Linked List
- C#中通过三边长判断三角形类型
- Win7 系统上安装SQL Server 2008一步一步图解教程
- STF开源框架之minicap工具
- java数据结构
- 扩号匹配问题(C程序设计进阶第3周)