POJ 2082

#include <iostream>  
#include <cstdio>  
using namespace std;  
  
const int N = 50005;  
  
struct Elem  
{  
    int height;  
    int width;  
};  
  
Elem stack[N];  
int top;  
  
int main()  
{  
    int height, width, n;  
    int ans, tot, tmp;  
    while (scanf("%d", &n) != EOF && n != -1)  
    {  
        top = 0;  
        ans = 0;  
        for (int i = 0; i < n; ++i)  
        {  
            scanf("%d%d", &width, &height);  
            tmp = 0;  
            while (top > 0 && stack[top - 1].height >= height)  
            {  
                tot = stack[top - 1].height * (stack[top - 1].width + tmp);  
                if (tot > ans) ans = tot;  
                tmp += stack[top - 1].width;  
                --top;  
            }  
            stack[top].height = height;  
            stack[top].width = width + tmp;  
            ++top;  
        }  
        tmp = 0;  
        while (top > 0)  
        {  
            tot = stack[top - 1].height * (stack[top - 1].width + tmp);  
            if (tot > ans) ans = tot;  
            tmp += stack[top - 1].width;  
            --top;  
        }  
        printf("%d\n", ans);  
    }  
    return 0;  
}

解决这题的关键是保持单调递增的入栈;

一,因为题目给的不可能是单调递增一组数,这时候你要想办法将中间的干掉;

二,比如,假设,所有的矩形z宽为1;有1 3 5 7 4 为高的矩形输入;首先输入1 3 5 7 是没问题的,如果要压入4; 那么必须把前面的5 7 干掉,干掉的同时,你也

要计算被干掉的面积,比如5,7,先弹出7,算出这个矩形面积的值比较,再存入;再就是5,这时候,要注意5,可以把7的合并,可以在上一步记录7的宽度;

弹出5,面积是10;这时候就可以存入4了;

三,最后处理一个单调递增的矩形就简单多了;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值