题目链接:轻点我一下~ >3<
求最大子矩形面积 = = 一开始很逗比的在求边界的时候写成了 - - l[i] = l[i]-1 r[i] = r[i] +1 n^2直接超时~ 正确的姿势应该是迭代 类似于动归的思想 还有注意求面积的时候会乘爆int~
AC代码如下:
//
// HDU 1506 Largest Rectangle in a Histogram
//
// Created by TaoSama on 2015-02-07
// Copyright (c) 2014 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n, l[N], r[N], h[N];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d", &n) == 1 && n) {
for(int i = 1; i <= n; ++i) {
scanf("%d", h + i);
l[i] = r[i] = i;
}
h[0] = h[n + 1] = -1;
for(int i = 1; i <= n; ++i)
while(h[l[i] - 1] >= h[i])
l[i] = l[l[i] - 1];
for(int i = n; i >= 1; --i)
while(h[r[i] + 1] >= h[i])
r[i] = r[r[i] + 1];
long long ans = -INF;
for(int i = 1; i <= n; ++i)
ans = max(ans, (r[i] - l[i] + 1LL) * h[i]);
printf("%I64d\n", ans);
}
return 0;
}