-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlargest_rectangle.cpp
55 lines (50 loc) · 1.27 KB
/
largest_rectangle.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
// added
#include <bits/stdc++.h>
using namespace std;
int largest_rectangle(vector<int> &hist) {
hist.push_back(0);
stack<pair<int, int>> st;
int ret = 0;
for (int i = 0; i < hist.size(); i++) {
if (st.empty() || st.top().first < hist[i]) {
st.emplace(hist[i], i);
} else {
int s = 0;
while (!st.empty() && st.top().first >= hist[i]) {
ret = max(ret, st.top().first * (i - st.top().second));
s = st.top().second;
st.pop();
}
st.emplace(hist[i], s);
}
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int h, w;
cin >> h >> w;
vector<vector<int>> b(h, vector<int>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int c;
cin >> c;
if (c == 0) {
if (i == 0) {
b[i][j] = 1;
} else {
b[i][j] = b[i - 1][j] + 1;
}
} else {
b[i][j] = 0;
}
}
}
int ret = 0;
for (int i = 0; i < h; i++) {
ret = max(ret, largest_rectangle(b[i]));
}
cout << ret << endl;
}