7. 木板墙

题目描述

考古学家在人迹罕至的一块平地上发现了由一堆木板拼成的墙。令人惊奇的是这些木板的宽度都相同!地下的部分都已腐烂,而地上的部分也有高有低,甚至有的地方根本没有木板,所以考古学家决定带走面积最大的长方形回去研究。

输入:
首先是整数n(1<=n<=100000),表示木板的块数。接下来是n个整数h1,…,hn, 其中0<=hi<=1000000000,它们按照从左到右的顺序表示木板的高度。每块木板的宽度都是1。
最后一个0表示程序的结束。
输出:
其中最大长方形的面积。

测试用例

输入

3 2000 2000 2000
7 1 2 5 6 1 3 3
0

输出

6000
10

分析

参考:单调栈

代码

#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;

struct Node
{
    int hight;
    int pre;
    int pos;
};

int main()
{
    int board_num;
    stack<Node>St;

    while (~scanf("%d", &board_num) && board_num)
    {
        long long sum_area = 0;

        for (int i = 1; i <= board_num; i++)
        {
            int temp_hight;
            int pre = 0;

            scanf("%d", &temp_hight);

            while (!St.empty() && St.top().hight > temp_hight)
            {
                Node temp = St.top();
                St.pop();
                pre += temp.pre + 1;
                sum_area = max(sum_area, (long long)temp.hight*(i - temp.pos + temp.pre));
            }

            Node push_node{ temp_hight,pre,i };
            St.push(push_node);
        }

        while (!St.empty())
        {
            Node temp = St.top();
            St.pop();
            sum_area = max(sum_area, (long long)temp.hight*(board_num + 1 - temp.pos + temp.pre));
        }

        printf("%lld\n", sum_area);
    }

    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值