#include <bits/stdc++.h>
using namespace std;
// Find the maximum rectangle area in a histogram
int maximalRectangleInHistogram(vector<int>& heights) {
int maxArea = 0;
stack<int> st;
for (int i = 0; i < heights.size(); i++) {
// Pop from stack while current height is less than stack top
while (!st.empty() && heights[st.top()] > heights[i]) {
int h = heights[st.top()];
st.pop();
// Width is from st.top()+1 to i-1 (or 0 to i-1 if stack is empty)
int w = st.empty() ? i : i - st.top() - 1;
maxArea = max(maxArea, h * w);
}
st.push(i);
}
// Pop remaining elements from stack
while (!st.empty()) {
int h = heights[st.top()];
st.pop();
int w = st.empty() ? (int)heights.size() : (int)heights.size() - st.top() - 1;
maxArea = max(maxArea, h * w);
}
return maxArea;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<string> grid(n);
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
// heights[j] = number of consecutive 1s above current row in column j
vector<int> heights(m, 0);
int maxArea = 0;
// Process each row
for (int i = 0; i < n; i++) {
// Update heights array
for (int j = 0; j < m; j++) {
if (grid[i][j] == '1') {
heights[j]++;
} else {
heights[j] = 0;
}
}
// Find max rectangle in current histogram
maxArea = max(maxArea, maximalRectangleInHistogram(heights));
}
cout << maxArea << endl;
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |