您的位置:首页 > 其它

9715 相邻最大矩形面积 单调栈

2016-11-14 12:38 260 查看

9715 相邻最大矩形面积

时间限制:1000MS 内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题 语言: G++;GCC;VC;JAVA

Description

在X轴上水平放置着 N 个条形图,这 N 个条形图就组成了一个柱状图,每个条形图都是一个矩形,每个
矩形都有相同的宽度,均为1单位长度,但是它们的高度并不相同。
例如下图,图1包含的矩形的高分别为2,1,4,5,1,3,3 单位长度,矩形的宽为1单位长度。


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 100000 + 20;
int dp[maxn][2];
int a[maxn];
int stack[maxn];
int id[maxn];
int n;
void work() {
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
a[n + 1] = -1;
int top = 1;
stack[top] = a[1];
id[top] = 1;
for (int i = 2; i <= n + 1; ++i) {
while (top >= 1 && a[i] < stack[top]) {
dp[id[top]][1] = i;
top--;
}
++top;
stack[top] = a[i];
id[top] = i;
}
top = 1;
stack[top] = a
;
id[top] = n;
for (int i = n - 1; i >= 0; --i) {
while (top >= 1 && a[i] < stack[top]) {
dp[id[top]][0] = i;
--top;
}
++top;
stack[top] = a[i];
id[top] = i;
}
//    for (int i = 1; i <= n; ++i) {
//        printf("%d***\n", dp[i][0]);
//    }
int ans = 0;
for (int i = 1; i <= n; ++i) {
int t = dp[i][1] - i + i - dp[i][0] - 1;
ans = max(ans, t * a[i]);
}
printf("%d\n", ans);
}

int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
while (scanf("%d", &n) != EOF) work();
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: