Description
Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all real numbers. wi, hi for i = 1 . . . n are some elements in N, and w0 = 0.
Define set B = {< x, y > | x, y ∈ R and there exists an index i > 0 such that 0 <= y <= hi ,∑0<=j<=i-1wj <= x <= ∑0<=j<=iwj}
Again, define set S = {A| A = WH for some W , H ∈ R+ and there exists x0, y0 in N such that the set T = { < x , y > | x, y ∈ R and x0 <= x <= x0 +W and y0 <= y <= y0 + H} is contained in set B}.
Your mission now. What is Max(S)?
Wow, it looks like a terrible problem. Problems that appear to be terrible are sometimes actually easy.
But for this one, believe me, it's difficult.
Input
The input consists of several test cases. For each case, n is given in a single line, and then followed by n lines, each containing wi and hi separated by a single space. The last line of the input is an single integer -1, indicating the end of input. You may assume that 1 <= n <= 50000 and w1h1+w2h2+...+wnhn < 109.
Output
Simply output Max(S) in a single line for each case.
Sample Input
3
1 2
3 4
1 2
3
3 4
1 2
3 4
-1
Sample Output
12
14
Source
Shanghai 2004 Preliminary
问题链接:POJ2028 ZOJ2422 Terrible Sets
问题描述:(略)
问题分析:
这也是一个求柱状图最大面积问题,类似于参考链接的那道题。基于参考链接的模板(白书的例子)原理实现本程序。
程序说明:(略)
参考链接:POJ2559 HDU1506 ZOJ1985 Largest Rectangle in a Histogram【堆栈+水题】
题记:(略)
AC的C语言程序如下:
/* POJ2028 ZOJ2422 Terrible Sets */
#include <stdio.h>
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define N 50000
int n;
int w[N], h[N];
int wsum[N + 1] = {0};
int L[N], R[N];
int st[N], ps; /* 堆栈与指针 */
long long solve()
{
int i;
/* 计算L */
ps = 0;
for (i = 0; i < n; i++) {
while (ps > 0 && h[st[ps - 1]] >= h[i]) ps--;
L[i] = ps == 0 ? 0 : (st[ps - 1] + 1);
st[ps++] = i;
}
/* 计算R */
ps = 0;
for (i = n - 1; i >= 0; i--) {
while (ps > 0 && h[st[ps - 1]] >= h[i]) ps--;
R[i] = ps == 0 ? n : st[ps - 1];
st[ps++] = i;
}
long long res = 0; /* 避免溢出用long long类型 */
for (i = 0; i < n; i++)
res = MAX(res, (long long) h[i] * (wsum[R[i]] - wsum[L[i]]));
return res;
}
int main()
{
int i;
while (~scanf("%d", &n) && n != -1) {
for (i = 0; i < n; i++) {
scanf("%d%d", &w[i], &h[i]);
wsum[i + 1] = wsum[i] + w[i];
}
printf("%lld\n", solve());
}
return 0;
}