Terrible Sets_单调栈

Posted 阿宝的锅锅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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\n",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\n",ans);
    }
    return 0;
}

 

以上是关于Terrible Sets_单调栈的主要内容,如果未能解决你的问题,请参考以下文章

poj2082 terrible sets 单调栈

POJ2082 Terrible Sets

POJ 2082 Terrible Sets

数据结构_栈和单调栈

[bzoj2086][Poi2010]Blocks_单调栈_双指针

单调栈[Luogu P4248]