hdu 1506 Largest Rectangle in a Histogram
来源:互联网 发布:东北林业大学网络教学 编辑:程序博客网 时间:2024/05/05 03:33
Largest Rectangle in a Histogram
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9075 Accepted Submission(s): 2501
Problem 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
Source
University of Ulm Local Contest 2003
Recommend
LL
dp。
思路:分别用两个数组记录对应的每个矩形左侧和右侧高度大于等于它本身的矩形的数量,这样用(高度*(左侧数量+右侧数量+1))即可表示对应高度的最大矩形面积。
100000的数据量用O(n2)的暴力肯定会T,分别从左到右更新左侧数量和从右到左更新右侧数量。刚开始用long long 一直wa,后来改成__int64就过了。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<ctype.h>#include<algorithm>#include<string>#define PI acos(-1.0)#define maxn 100005#define INF 1<<25__int64 hh[maxn];__int64 le[maxn];__int64 ri[maxn];using namespace std;int main(){ __int64 tot; while(scanf("%I64d",&tot)) { if(tot==0) break; memset(hh,0,sizeof(hh)); memset(le,0,sizeof(le)); memset(ri,0,sizeof(ri)); le[0]=0; ri[tot-1]=0; ll i,j; for(i=0;i<tot;i++) { scanf("%I64d",&hh[i]); j=i-1; while(hh[j]>=hh[i]&&j>=0) { j-=(le[j]+1); } le[i]=i-j-1; } for(i=tot-1;i>=0;i--) { j=i+1; while(hh[j]>=hh[i]&&j<=tot-1) { j+=(ri[j]+1); } ri[i]=j-i-1; } __int64 ans=0; for(i=0;i<tot;i++) { if((ri[i]+le[i]+1)*hh[i]>ans) ans=(ri[i]+le[i]+1)*hh[i]; } printf("%I64d\n",ans); }}
- hdu 1506 Largest Rectangle in a Histogram
- HDU 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- HDU 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- HDU 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- HDU 1506 Largest Rectangle in a Histogram
- HDU 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- HDU 1506 Largest Rectangle in a Histogram
- HDU 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- Linux tar -cvf与tar cvf有什么区别
- Sending HTML content in an email using PHP
- 程序在内存中存储详情(Text、Data、BSS、Heap、Stack....)
- 那些代码中我们常犯的错误你有木有。。。
- jQuery jcrop插件截图使用方法
- hdu 1506 Largest Rectangle in a Histogram
- Accelerated C++ Exercise 7-2
- 利用SynchronizationContext.Current在线程间同步上下文
- 成员函数的重载、覆盖与隐藏
- java抽取word,pdf的四种武器
- COM,ATL,WTL,OLE,STL,MFC
- Oracle 表分区
- hdu1297Children’s Queue(递推+大数)
- 常用数据库JDBC连接写法