HUST 1644 (思维)

1644 - 第四届“恒生杯”程序设计大赛决赛 B

时间限制:1秒 内存限制:256兆

70 次提交 18 次通过
题目描述

Given N non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

 

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

 

 

The largest rectangle is shown in the shaded area, which has area = 10 unit.

 

For example,

Given height = [2,1,5,6,2,3],

 

return 10.

输入

 

The first line contains the number of test cases T. T test cases follow. Each case contains an integer N, followed by integers height[1],...,height[N] on the second line.

 

1 <= T <= 100 

1 <= N <= 100000 

 

1 <= height[i] <= 1000

输出

 

Output T lines, one for each test case, containing the largest rectangle area.


这题是在校内训练的时候给学弟在hust上找的,当时觉得人过的不少,难度应该一般。结果确实没人做出来了,实际上确实是一道不错的题,hust上的题基本上是从leetcode[84]抄过来的,但数据好像不强。

题解和代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define mst(a,k)  memset(a,k,sizeof(a));
#define LL long long
#define eps 1e-8
using namespace std;
/*
题目要求最大面积,即要找到一对左右边界使得其中面积正好最大。
我们维护一个递增栈:从栈底到栈顶,元素值递增
现在从左到右把柱子扫一遍,如果栈为空或者heigt[i]>=height[top],则入栈
否则:将栈顶元素出栈。
对于这个元素来说,它可形成的面积的左边界就是栈中的它下面的那个元素(不包括它)(因为栈是递增的,所以它下面的那个元素比它小)
右边界即它当前迫使它出栈的这个柱子的前一个。因为如果某两个元素同时在栈中,先出栈的那个元素肯定是比后出栈的元素大的,对不对?所以在
某元素出栈之前,它前面还有元素出栈了(同时和这个元素处在栈中过),那前面出栈的这些元素肯定都是可以与它组成面积的(高度比它高)
这样,对于每一个出栈的元素,我们都可以很轻松的找到它的边界,也就算出了面积。只要在扫的过程中记录下最大值就可以了

但是这里还要注意一点:当把栈顶弹出后,栈为空了,它的左边界即为0,因为栈都被弹空了,说明左边已经没有比它小的数了。

*/
int height[100005];
int main()
{
    int t,n;
    scan(t);
    while(t--)
    {
        scan(n);
        REP(i,0,n) scan(height[i]);
        height[n]=0; //在最后添加一个高度为0的柱子,使得最后栈能“清空”
        int ans=0;
        stack<int>st;
        for(int i = 0; i <= n; i++)
        {
            while(!st.empty() && height[i] < height[st.top()])
            {
                int val = st.top();
                st.pop(); //出栈!!
                //ans = max(ans, height[val]*(i-1-(st.empty()?-1:st.top())));
                if(st.empty())  //如果为空,左边界为0,右边界i-1,长度为i-1+1。
                    ans = max(ans,height[val]*i);
                else    //不为空,左边界新的栈顶+1,右边界i-1,长度i-1-(st.top+1)+1
                    ans = max(ans,height[val]*(i-1-st.top()));
            }
            st.push(i);
        }
        printf("%d\n",ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值