题目看了半天也看不懂,反正和1985那个题差不多就是了,都是求并排不等高矩形的最大矩形面积,唯一区别就是那个题宽度固定为1,本题宽度不固定,代码都差不多,加上宽度的处理就行了,把那题代码拿过来稍微改改就A了。。
#include<stdio.h>
int main()
{
int n, w[50000], h[50000], left[50000], right[50000], lw[50000], rw[50000];
while (scanf("%d", &n), n != -1)
{
int i;
for (i = 0; i < n; i++)
{
scanf("%d %d", &w[i], &h[i]);
left[i] = i;
right[i] = i;
lw[i] = rw[i] = w[i];
}
int temp;
for (i = 0; i < n; i++)
{
temp = i;
while (temp - 1 >= 0 && h[temp - 1] >= h[i])
{
lw[i] += lw[temp - 1];
temp = left[temp - 1];
}
left[i] = temp;
}
for (i = n - 1; i >= 0; i--)
{
temp = i;
while (temp + 1 < n && h[temp + 1] >= h[i])
{
rw[i] += rw[temp + 1];
temp = right[temp + 1];
}
right[i] = temp;
}
int max = 0, res;
for (i = 0; i < n; i++)
{
res = (lw[i] + rw[i] - w[i]) * h[i];
if (res > max)
max = res;
}
printf("%d\n", max);
}
return 0;
}