Poj 2082(单调栈)

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.

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

Shanghai 2004 Preliminary
题意就是给你一个排成一排的小矩形,要你找出他们组成的最大的矩形。用单调栈预处理出每个矩形往左能到达的高度大于它的最远点,和往右的最远点即可。
#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;
}


你可能感兴趣的:(Poj 2082(单调栈))