最大子矩阵问题总结
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Largest Rectangle in a Histogram
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 53 Accepted Submission(s) : 8
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.
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
Sample Output
8
4000
这个题目是众多子矩阵问题的源头,所以要好好总结一下。
重点:对于每个点,记算出他所能向左和向右延伸的最大边界,该长度乘以该点高度就是该店所能呈现的最大值,最后扫描一边找出最大的。
对于某个点,如果该点的高度比左边的一个小,那么该点的左边界就是这个点左边的点的左边界。右边界亦是如此。
#include
#include
#include
#include
using namespace std;
const int N=100010;
long long a[N],l[N],r[N];
int main()
{
int n;
while(cin>>n&&n){
int i,j;
for(i=1;i<=n;i++){
//cin>>a[i];
scanf("%I64d",&a[i]);
l[i]=r[i]=i;
}
for(i=1;i<=n;i++){ //延伸左边界
while(l[i]>1&&a[i]<=a[l[i]-1])
l[i]=l[l[i]-1];
}
for(i=n;i>0;i--){ //延伸右边界
while(r[i] r[i]=r[r[i]+1]; } long long ans=0; for(i=1;i<=n;i++){ ans=max(ans,(r[i]-l[i]+1)*a[i]); } cout< } return 0; } City Game Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 12 Accepted Submission(s) : 5 Problem Description Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees,factories and buildings. There is still some space in the area that is unoccupied. The strategic task of his game is to win as much rent money from these free spaces. To win rent money you must erect buildings, that can only be rectangular, as long and wide as you can. Bob is trying to find a way to build the biggest possible building in each area. But he comes across some problems – he is not allowed to destroy already existing buildings, trees, factories and streets in the area he is building in. Each area has its width and length. The area is divided into a grid of equal square units.The rent paid for each unit on which you're building stands is 3$. Your task is to help Bob solve this problem. The whole city is divided into K areas. Each one of the areas is rectangular and has a different grid size with its own length M and width N.The existing occupied units are marked with the symbol R. The unoccupied units are marked with the symbol F Input The first line of the input contains an integer K – determining the number of datasets. Next lines contain the area descriptions. One description is defined in the following way: The first line contains two integers-area length M<=1000 and width N<=1000, separated by a blank space. The next M lines contain N symbols that mark the reserved or free grid units,separated by a blank space. The symbols used are: R – reserved unit F – free unit In the end of each area description there is a separating line. Output For each data set in the input print on a separate line, on the standard output, the integer that represents the profit obtained by erecting the largest building in the area encoded by the data set. Sample Input 2 5 6 R F F F F F