hdu 1506
对于每一个直方块,只要能知道以它的高度往左(往右)最大能走多少,再枚举一遍所有的直方块即可。而往左(往右)最大能走多少可以用动态规划的方法实现,开数组left(right),对于每一个方块,初始left[i](right[i])为i,然后用迭代的方法往左(右)看,即可求出。思路想出来了,代码就很简单了。
/*
* hdu1506/win.cpp
* Created on: 2011-10-1
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 100010;
int height[MAXN], left[MAXN], right[MAXN];
void work();
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
work();
return 0;
}
void work() {
int N;
long long ans, temp;
height[0] = -1;
while (scanf("%d", &N) == 1 && N > 0) {
for (int i = 1; i <= N; i++) {
scanf("%d", &height[i]);
left[i] = i;
right[i] = i;
}
height[N + 1] = -0x7fffffff;
for (int i = 1; i <= N; i++) {
while (height[left[i] - 1] >= height[i]) {
left[i] = left[left[i] - 1];
}
}
for (int i = N; i >= 1; i--) {
while (height[right[i] + 1] >= height[i]) {
right[i] = right[right[i] + 1];
}
}
ans = -1;
for (int i = 1; i <= N; i++) {
temp = right[i] - left[i] + 1;
temp *= height[i];
if (temp > ans) {
ans = temp;
}
}
printf("%I64d\n", ans);
}
}