题目大意:
可以理解为有很多的矩形从左至右连接在一起,每个矩形的大小可能不同,问怎么样可以取得最大一个矩形
相当于把所有的矩形从左至右放到一个X轴上,然后其中最大可以画出来的矩形
题目算法:
利用堆栈,始终保持堆栈中的元素(矩形)的高度为从小到大
如果新加入的矩形的高度要小于栈定的高度
则把栈顶的矩阵出栈
直到满足条件(栈顶的元素的高度 小于要加进来的高度)
当然一直出栈的时候要记住两件事
第一件事,每当即将出栈之前,累计长度length
第二件事,判断length * 即将出栈的高度 > maxValue(本轮出栈所能取得的最大值,初值为0),满足即将maxValue设置为其值
第三件事,判断maxValue > result (结果值,初始值为-1,不会随另外一个矩形的加入而改变,每个case才会重新赋值-1)
这样最后就可以把要入栈的矩阵的长度 另外再加上length,然后入栈
继续添加下一个矩阵
所有添加完以后,用刚才的方法,从栈顶开始,逐个出栈,同时:
1.出栈之前累积长度length
2.判断length*即将出栈的高度 >maxValue 成立即赋值给maxvalue
所有都出栈后maxValue与result比较,取大者为结果
Terrible Sets
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 2621 | Accepted: 1325 |
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 w1h1+w2h2+...+wnhn < 109.
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
#include <iostream>
#include <stack>
using namespace std;
struct Node
{
int x,y;
};
stack<Node> s;
int main()
{
int N,result;
while(scanf("%d",&N) && N!=-1)
{
result=-1;
for(int i=0;i<N;i++)
{
Node n;
scanf("%d %d",&n.x,&n.y);
if(s.empty() )
s.push(n);
else
{
if(s.top().y < n.y)
s.push(n);
else
{
int length=0,maxValue=-1;
while( !(s.top().y < n.y ) )
{
length+=s.top().x;
if( length*s.top().y > maxValue)
maxValue=length * s.top().y;
if(maxValue>result)
result=maxValue;
s.pop();
if( s.empty())
break;
}
n.x+=length;
s.push(n);
}//end if-else top.y<n.y
}//end if-else s.empty()
}//end process N th Matrix
//开始扫描计算最大值
int maxValue=-1,length=0;
while(!s.empty() )
{
length+=s.top().x;
if(length * s.top().y > maxValue)
maxValue = length * s.top().y;
s.pop();
}
if(maxValue>result)
result=maxValue;
printf("%d\n",result);
}
return 0;
}

本文介绍了一种通过堆栈实现的高效算法来寻找由多个矩形组成的最大单一矩形区域。该算法保持堆栈中矩形高度递增,并在新矩形高度较低时,调整堆栈并计算可能的最大矩形面积。
183

被折叠的 条评论
为什么被折叠?



