Terrible Sets
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 2778 | Accepted: 1413 |
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
Source
题意就是给你一个排成一排的小矩形,要你找出他们组成的最大的矩形。用单调栈预处理出每个矩形往左能到达的高度大于它的最远点,和往右的最远点即可。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
const int maxn = 50000 + 5;
const int INF = 2000000000;
typedef pair<int, int> P;
typedef long long LL;
int w[maxn],h[maxn];
int l[maxn],r[maxn];
int sum[maxn];
stack<P> S;
int main(){
int n;
while(scanf("%d",&n)){
if(n == -1) break;
for(int i = 0;i < n;i++){
scanf("%d%d",&w[i],&h[i]);
}
sum[0] = w[0];
for(int i = 1;i < n;i++) sum[i] = sum[i-1]+w[i];
while(!S.empty()) S.pop();
for(int i = 0;i < n;i++){
while(!S.empty() && h[i] <= S.top().first) S.pop();
if(S.size() == 0) l[i] = sum[i];
else l[i] = sum[i]-sum[S.top().second];
S.push(P(h[i],i));
}
while(!S.empty()) S.pop();
for(int i = n-1;i >= 0;i--){
while(!S.empty() && h[i] <= S.top().first) S.pop();
if(S.size() == 0) r[i] = sum[n-1]-sum[i-1];
else r[i] = sum[S.top().second-1]-sum[i-1];
S.push(P(h[i],i));
}
int ans = 0;
for(int i = 0;i < n;i++){
ans = max(ans,h[i]*(l[i]+r[i]-w[i]));
}
printf("%d\n",ans);
}
return 0;
}
最大矩形面积算法
本文介绍了一种通过单调栈预处理方法解决寻找由一系列小矩形组成的最大矩形的问题。该算法首先定义了一系列数学集合,并将问题转化为计算特定条件下的最大矩形面积。
1752

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



