POJ 2082 Terrible Sets(单调栈)

题目链接:http://poj.org/problem?id=2082

 

题意:给定坐标系上一系列相邻的矩形,高度不等,求把这些矩形连起来看能够得到的最大矩形的面积

 

思路:利用单调栈,去维护一个高度不下降的矩形,如果遇到一个高度比栈顶低的矩形就出栈,并计算出栈矩形的面积,最后再逐渐计算出最终维护下来的栈内的矩形的面积

 

 

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <functional>
#include <utility>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <set>
#include <cmath>
#include <stdlib.h>
#include <climits>

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment (linker, "/STACK:1024000000,1024000000")

typedef long long ll;

using namespace std;

const int maxn = 50100;

struct node
{
    int w, h;
} sta[maxn];

int main()
{
    int n;
    while (~scanf("%d", &n) && n != - 1)
    {
        int ans = 0;
        int top = 0;
        for (int i = 1; i <= n; i++)
        {
            int w, h;
            int tot = 0;
            scanf("%d%d", &w, &h);
            while (top > 0 && h < sta[top - 1].h)
            {
                tot += sta[top - 1].w;
                if (ans < tot * sta[top - 1].h)
                    ans = tot * sta[top - 1].h;
                top--;
            }
            sta[top].h = h;
            sta[top++].w = w + tot;
        }
        int tot = 0;
        while (top)
        {
            tot += sta[top - 1].w;
            if (ans < tot * sta[top - 1].h)
                ans = tot * sta[top - 1].h;
            top--;
        }
        cout << ans << endl;
    }
    return 0;
}

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值