forked from yogykwan/acm-challenge-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoj2082.cpp
59 lines (50 loc) · 1.26 KB
/
poj2082.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* POJ 2082: Terrible Sets
* 题意:一排相邻的矩形,给出各自的高度和宽度。求一个面积最大的矩形,被之前的矩形完全包含。
* 类型:栈
* 算法:用栈得到某个矩形靠它最近的比它矮的左边和右边,得到它对应的宽度,相乘为待选解。
*/
#include <cstdio>
#include <stack>
using namespace std;
int h[50010], w[50010];
int s[50010];
int l[50010], r[50010];
stack<int> st;
int main() {
int n;
while (scanf("%d", &n) != EOF && n != -1) {
int ans = 0;
s[0] = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &w[i], &h[i]);
s[i] = s[i - 1] + w[i];
}
h[0] = h[n + 1] = -1;
while (!st.empty()) st.pop();
for (int i = 1; i <= n + 1; ++i) {
while (!st.empty()) {
if (h[st.top()] > h[i]) {
r[st.top()] = i;
st.pop();
} else break;
}
st.push(i);
}
while (!st.empty()) st.pop();
for (int i = n; i >= 0; --i) {
while (!st.empty()) {
if (h[st.top()] > h[i]) {
l[st.top()] = i;
st.pop();
} else break;
}
st.push(i);
}
for (int i = 1; i <= n; ++i) {
ans = max(ans, h[i] * (s[r[i] - 1] - s[l[i]]));
}
printf("%d\n", ans);
}
return 0;
}