1.题目
Terrible Sets
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5239 | Accepted: 2660 |
Description
Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all real numbers. wi, hi for i = 1 . . . n are some elements in N, and w0 = 0.
Define set B = {< x, y > | x, y ∈ R and there exists an index i > 0 such that 0 <= y <= hi ,∑ 0<=j<=i-1wj <= x <= ∑ 0<=j<=iwj}
Again, define set S = {A| A = WH for some W , H ∈ R + and there exists x0, y0 in N such that the set T = { < x , y > | x, y ∈ R and x0 <= x <= x0 +W and y0 <= y <= y0 + H} is contained in set B}.
Your mission now. What is Max(S)?
Wow, it looks like a terrible problem. Problems that appear to be terrible are sometimes actually easy.
But for this one, believe me, it's difficult.
Define set B = {< x, y > | x, y ∈ R and there exists an index i > 0 such that 0 <= y <= hi ,∑ 0<=j<=i-1wj <= x <= ∑ 0<=j<=iwj}
Again, define set S = {A| A = WH for some W , H ∈ R + and there exists x0, y0 in N such that the set T = { < x , y > | x, y ∈ R and x0 <= x <= x0 +W and y0 <= y <= y0 + H} is contained in set B}.
Your mission now. What is Max(S)?
Wow, it looks like a terrible problem. Problems that appear to be terrible are sometimes actually easy.
But for this one, believe me, it's difficult.
Input
The input consists of several test cases. For each case, n is given in a single line, and then followed by n lines, each containing wi and hi separated by a single space. The last line of the input is an single integer -1, indicating the end of input. You may assume that 1 <= n <= 50000 and w
1h
1+w
2h
2+...+w
nh
n < 10
9.
Output
Simply output Max(S) in a single line for each case.
Sample Input
3 1 2 3 4 1 2 3 3 4 1 2 3 4 -1
Sample Output
12 14
大意:
紧贴x轴有一些相互挨着的矩形(每个矩形的宽、高均不定,类似水滴),给定每个矩形的长宽,问它们相互作用下可以形成的最大连续子矩形的面积是多少?
2.思路
要求最大面积的子矩阵,可以通过栈将每个矩形入栈,保持栈中元素按高度递增
如果入栈矩形的高比栈顶矩形的高度小,则一直出栈至栈顶矩形的高小于入栈矩形。
在出栈时要累计记录每个出栈矩形的宽,以当前出栈矩形的高 * 累计的宽度 = 当前出栈矩阵所能得到的最大子矩阵面积
于此同时,还得将入栈的矩形宽度更改为累计的宽度,即合并出栈矩阵与入栈矩阵(以入栈矩阵的高),
也可以理解为保存当前入栈矩阵与之前出栈矩阵所构成的子矩阵
如果入栈矩形的高比栈顶矩形的高度小,则一直出栈至栈顶矩形的高小于入栈矩形。
在出栈时要累计记录每个出栈矩形的宽,以当前出栈矩形的高 * 累计的宽度 = 当前出栈矩阵所能得到的最大子矩阵面积
于此同时,还得将入栈的矩形宽度更改为累计的宽度,即合并出栈矩阵与入栈矩阵(以入栈矩阵的高),
也可以理解为保存当前入栈矩阵与之前出栈矩阵所构成的子矩阵
3.代码
static class Rec{
int w;
int h;
public Rec(int w,int h){
this.w = w;
this.h = h;
}
}
static void poj2082(){
Scanner scanner = new Scanner(System.in);
int n = 0;
int totalWidth = 0;
int curArea = 0;
int maxArea = 0;
while((n = Integer.parseInt(scanner.nextLine())) != -1){
Stack<Rec> s = new Stack<Rec>();
for(int i=0;i<n;i++){
String[] input = scanner.nextLine().split(" ");
Rec rec = new Rec(Integer.parseInt(input[0]),
Integer.parseInt(input[1]));
if(s.isEmpty() || s.peek().h<=rec.h){
s.push(rec);
}else{ //否则 统计出栈矩形的宽度,以得当前最大子矩形面积
totalWidth = 0;
curArea = 0;
while(!s.isEmpty() && s.peek().h>rec.h){
totalWidth += s.peek().w;
curArea = totalWidth * s.peek().h;
if(maxArea < curArea)
maxArea = curArea;
s.pop();
}
//将出栈矩阵的宽度与入栈矩阵的宽度合并,得当前最大子矩阵入栈
rec.w += totalWidth;
s.push(rec);
}
}
totalWidth = 0;
curArea = 0;
//输入结束,同理遍历栈内的矩阵
while(!s.isEmpty()){
totalWidth += s.peek().w;
curArea = totalWidth * s.peek().h;
if(maxArea < curArea)
maxArea = curArea;
s.pop();
}
System.out.println(maxArea);
}
}