-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangle_matrix.cpp
More file actions
79 lines (60 loc) · 1.1 KB
/
rectangle_matrix.cpp
File metadata and controls
79 lines (60 loc) · 1.1 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, m;
cin>>n >> m;
vector<vector<int>> mt(n, vector<int>(m));
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>mt[i][j];
}
}
vector<int> prevr(m, 0);
int mx = 0;
for(int i=0; i<n; i++){
stack<int> st;
vector<int> left(m, 0), right(m, 0);
for(int k=0; k<m; k++){
if(mt[i][k]){
prevr[k] += 1;
}
else{
prevr[k] = 0;
}
}
for(int j=0; j<m; j++){
if(st.empty()){
st.push(j);
left[j] = 0;
}
else{
while(!st.empty() && prevr[st.top()] >= prevr[j]){
st.pop();
}
left[j] = st.empty() ? 0 : st.top() + 1;
st.push(j);
}
}
while(!st.empty()){
st.pop();
}
for(int j=m-1; j>=0; j--){
if(st.empty()){
st.push(j);
right[j] = m -1;
}
else{
while(!st.empty() && prevr[st.top()] >= prevr[j]){
st.pop();
}
right[j] = st.empty() ? m-1 : st.top() - 1;
st.push(j);
}
}
for(int j=0; j<m; j++){
mx = max(mx, prevr[j] * abs(right[j] - left[j] + 1));
}
}
cout<< "max : " << mx;
return 0;
}