• HDU


    题意:已知n个高度不一、宽度相同的矩形并列排放,求所形成的图形中最大的矩形面积。

    分析:

    1、对于每一个矩形,分别算出它左边连续比它高的矩形中最左边的下标,右边同理。通过(r[i] - l[i] + 1) * a[i]比较得到最大的矩形面积。

    2、将一组高度依次降低的矩形看成一个整体,如果该矩形比这个整体最矮的矩形都矮,长度可直接延伸过去,通过tmp = l[tmp - 1],依次向左比较,直到找到最左边的下标。

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define lowbit(x) (x & (-x))
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 100000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int a[MAXN], l[MAXN], r[MAXN];
    int main(){
        int n;
        while(scanf("%d", &n) == 1){
            if(n == 0) return 0;
            for(int i = 0; i < n; ++i){
                scanf("%d", &a[i]);
            }
            l[0] = 0;
            for(int i = 1; i < n; ++i){
                int tmp = i;
                while(tmp > 0 && a[tmp - 1] >= a[i]) tmp = l[tmp - 1];
                l[i] = tmp;
            }
            r[n - 1] = n - 1;
            for(int i = n - 2; i >= 0; --i){
                int tmp = i;
                while(tmp < n - 1 && a[tmp + 1] >= a[i]) tmp = r[tmp + 1];
                r[i] = tmp;
            }
            LL ans = 0;
            for(int i = 0; i < n; ++i){
                ans = max(ans, (LL)(r[i] - l[i] + 1) * a[i]);
            }
            cout << ans << endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    开发者论坛一周精粹(第九期)
    你刚吃的兰州牛肉面_背后就藏着大数据
    《C++覆辙录》——1.9:使用糟糕的语言
    老司机带你用MaxCompute和表格存储玩转车联网数据
    Gartner最新发布:2017年十大战略技术趋势
    js的事件的三个阶段,事件委托的原理
    Spring的AOP1
    了解SQL注入攻击
    了解XSS攻击
    了解Serialization
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7341431.html
Copyright © 2020-2023  润新知