Terrible Sets
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other)
Total Submission(s) : 26 Accepted Submission(s) : 14
Problem 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<sub>1</sub>h<sub>1</sub>+w<sub>2</sub>h<sub>2</sub>+...+w<sub>n</sub>h<sub>n</sub> < 10<sup>9</sup>.
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
PKU
题意:
在一个轴上,有不同宽度和高度的矩形,让你求最大面积。
思路:
和前面做过的一个题目一样,只不过是稍微变了一点,还是用两个数组分别记录一个数左边和右边大于它的数,注意此处用个小技巧,即宽度用前缀数组和表示,这样容易计算。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#include <iomanip>
#define maxn 50010
#define mod 1000000007
#define INF 0x3f3f3f3f
#define exp 1e-6
#define pi acos(-1.0)
using namespace std;
int h[maxn],w,lit[maxn],rit[maxn],sum[maxn];
int main()
{
ios::sync_with_stdio(false);
int n,i;
while(cin>>n)
{
if(n==-1)break;
sum[0]=0;
for(i=1;i<=n;i++){cin>>w>>h[i];sum[i]=sum[i-1]+w;lit[i]=rit[i]=i;}
h[0]=h[n+1]=-1;
for(i=1;i<=n;i++)
{
while(h[i]<=h[lit[i]-1])lit[i]=lit[lit[i]-1];
}
for(i=n;i>=1;i--)
{
while(h[i]<=h[rit[i]+1])rit[i]=rit[rit[i]+1];
}
int maxx=0;
for(i=1;i<=n;i++)
maxx=max(maxx,h[i]*(sum[rit[i]]-sum[lit[i]-1]));
cout<<maxx<<endl;
}
return 0;
}