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;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7341431.html