Terrible Sets_单调栈

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 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<stdio.h>
#include<string.h>
using namespace std;
const int N=50005;
int n;
struct node
{
    int w,h;
}a[N];
int main()
{
    while(scanf("%d",&n))
    {
        int ans=0;
        if(n==-1) break;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].w,&a[i].h);
        }
        for(int i=1;i<=n;i++)
        {
            int sum=0;
            for(int j=i;j>=1;j--)
            {
                if(a[j].h>=a[i].h)
                   sum+=a[j].w;
                else break;
            }
            for(int j=i+1;j<=n;j++)
            {
                if(a[j].h>=a[i].h)
                    sum+=a[j].w;
                else break;
            }
            ans=max(ans,sum*a[i].h);
        }
        printf("%d
",ans);
    }
    return 0;
}

豪华版(单调栈):

#include<iostream>
#include<stack>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=50005;
int n;
struct node
{
    int h,w;
}st[N];
int cnt;
int main()
{
    while(scanf("%d",&n))
    {
        if(n==-1) break;
        int ans=0;
        int h,w;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&w,&h);
            if(h>=st[cnt].h)
            {
                st[++cnt].w=w;
                st[cnt].h=h;
            }
            else
            {
                int sum=0;
                while(st[cnt].h>=h)
                {
                    sum+=st[cnt].w;
                    ans=max(ans,sum*st[cnt].h);
                    cnt--;
                }
                sum+=w;
                st[++cnt].w=sum;
                st[cnt].h=h;
            }
        }
        int sum=0;
        while(cnt>0)//清空栈
        {
            sum+=st[cnt].w;
            ans=max(ans,sum*st[cnt].h);
            cnt--;
        }
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/iwantstrong/p/5936970.html