Terrible Sets
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4377 | Accepted: 2272 |
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
Source
题意:给你一定长度矩形 宽、高,按照输入的顺序放在x轴,无缝
求最大的连同的矩形面积,是不是一脸懵逼,都一样我看懂题目一个
小时,做出来快3个小时
其实仔细想想:如果题目按照高度从小到大的顺序输入是不是就很简单
其实这个题目自我感觉就是它的变形,代码也是这样写的,按照高度
从小到大的顺序,进行面积的不断更新,栈的知识
求最大的连同的矩形面积,是不是一脸懵逼,都一样我看懂题目一个
小时,做出来快3个小时
其实仔细想想:如果题目按照高度从小到大的顺序输入是不是就很简单
其实这个题目自我感觉就是它的变形,代码也是这样写的,按照高度
从小到大的顺序,进行面积的不断更新,栈的知识
<span style="font-size:18px;">#include<cstdio>
#include<stack>
using namespace std;
struct node
{
int w;//宽
int h;//高
};
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n!=-1)
{
stack<node>sta;
int i=0,ans=0,a,s1;//输出最后的面积
int lasth=0,s=0;//记录栈中最上面的高度 ,记录面积用于不断的更新
for(i=0;i<n;++i)
{
node now;
scanf("%d%d",&now.w,&now.h);
if(now.h>lasth)
sta.push(now);//高度按照从大到小的顺序堆放
else
{
a=0,s1=0;//当前的宽度 和当前的面积
while(!sta.empty()&&sta.top().h>now.h)
{
a=a+sta.top().w;
s1=a*sta.top().h;
sta.pop();
if(s1>ans)
ans=s1;
}
now.w+=a;
sta.push(now);//新生成压进去 主要是宽度
}
lasth=sta.top().h;//高度更新
}
a=0;s1=0;
while(!sta.empty())//最后一次更新面积
{
a+=sta.top().w;
s1=a*sta.top().h;
if(s1>ans)
ans=s1;
sta.pop();
}
printf("%d\n",ans);
}
}
</span>