-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxareaRectfullof1.cpp
More file actions
77 lines (73 loc) · 1.39 KB
/
MaxareaRectfullof1.cpp
File metadata and controls
77 lines (73 loc) · 1.39 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
75
76
77
#include <iostream>
#include <stack>
using namespace std;
int v[100];
int a[100][100];
long int findmaxhist(int b[],int m)
{
stack <int> s;
int right[100],left[100];
for(int i=1;i<=m;i++)
{
right[i]=m+1;
left[i]=0;
}
for(int i=1;i<=m;i++)
{
while(!s.empty() && b[s.top()]>b[i])
{
right[s.top()]=i;
s.pop();
}
s.push(i);
}
while(!s.empty())
s.pop();
for(int i=m;i>0;i--)
{
while(!s.empty() && b[s.top()]>b[i])
{
left[s.top()]=i;
s.pop();
}
s.push(i);
}
long int area,maxarea=0;
for(int k=1;k<=m;k++)
{
area=v[k]*(right[k]-left[k]-1);
if(area>maxarea)
maxarea=area;
}
return maxarea;
}
long int maxrect(int n,int m)
{
long int maxarea=0,carea;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]==0)
v[j]=0;
else
v[j]+=a[i][j];
}
carea=findmaxhist(v,m);
if(carea>maxarea)
{
maxarea=carea;
}
}
return maxarea;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
cout<<maxrect(n,m);
return 0;
}