Terrible Sets(Stack 栈)

Terrible Sets
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5360 Accepted: 2728

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

题意:给你n个连续的矩形的宽和高,问最大能构成的矩形的面积是多少。

题解:弱鸡终于要开始学习数据结构的有关知识了,首先是一道栈的问题,让矩形入栈,保持栈中的元素高度递增,若入栈的元素比之前最后一个元素高度低,则将之前的大于该元素高度的所有元素出栈,并记录此时出栈元素能构成的矩形的面积最大值(出栈的元素一定递减),最后再将所有元素出栈记录面积最大值即可。

#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;

inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

struct node
{
    int w,h;
};

int main()
{
    int n;
    node p;
    while(scanf("%d",&n),n!=-1)
    {
        stack<node>s;
        int last=0,ans=0;
        int temp=0,tot=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&p.w,&p.h);
            if(p.h>=last)
                s.push(p);
            else
            {
                temp=0,tot=0;
                while(!s.empty() && s.top().h>p.h)
                {
                    tot+=s.top().w;
                    temp=tot*s.top().h;
                    ans=max(ans,temp);
                    s.pop();
                }
                p.w+=tot;
                s.push(p);
            }
            last=p.h;
        }

        temp=0,tot=0;
        while(!s.empty())
        {
            node pp=s.top();
            tot+=pp.w;
            temp=tot*pp.h;
            ans=max(ans,temp);
            s.pop();
        }

        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值