Largest Rectangle in a Histogram
来源:互联网 发布:coex mall有mac么 编辑:程序博客网 时间:2024/05/21 19:20
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
4000
这道题采用栈将递增的高度保存下来就行,碰到递减就将前面的面积全部算出来就可以,
不过我开始一直将x搞错了,x应该是当前坐标,保存x坐标时只需要保存前面比当前高度
低的x坐标。
#include <stdio.h>#define LL long longconst 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[n] = -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 longusing 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;}
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram
- Problem H: Largest Rectangle in a Histogram
- POJ2559 Largest Rectangle in a Histogram(DP)
- hdu 1506 Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- android 自定义 环形进度条
- fcntl
- Beaglebone Black(8)运用Python来进行控制IO,ADC,PWM
- OC -分数
- POJ 2318 TOYS || POJ 2398 Toy Storage
- Largest Rectangle in a Histogram
- Intellij IDEA 14代码错误提示如何调出来
- WebRTC VideoEngine 本地Video数据处理-VideoCaptureInputTest
- 无法解析的外部符号 "class boost::system::error_category const & __cdecl boost::system::generic_category(void)
- 2014-2015 ACM-ICPC, Asia Tokyo Regional Contest A题 Bit String Reordering(暴力)
- muduo库阅读(6)——当前线程
- 点击MyEclipse中Tomcat发布项目按钮没有反应
- aq-Android版本检测升级
- HTML5异步上传图片(支持预览和进度条),使用其他按钮触发file onclick事件,解决某些浏览器无法触发的问题