This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathC_Eva.c
74 lines (67 loc) · 1.46 KB
/
C_Eva.c
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 <stdio.h>
#include <stdlib.h>
//stack implementation
struct stack{
int* data;
int top;
};
int empty(struct stack* s){
return (s->top==-1);
}
int top(struct stack* s){
return s->data[s->top];
}
void pop(struct stack* s){
s->top--;
}
void push(int n, struct stack* s){
s->top++;
s->data[s->top]=n;
}
//calculating maximum rectangle for each row
int maxarea(int n, int a[]){
int area, max=0, temp, i;
struct stack* s=(struct stack*)malloc(sizeof(struct stack));
s->top=-1;
s->data=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++){
while(!empty(s) && a[top(s)]>a[i]){
temp=top(s);
pop(s);
if(empty(s)) area=a[temp]*i;
else area=a[temp]*(i-top(s)-1);
if(max<area) max=area;
}
if(empty(s) || a[top(s)]<=a[i]){
push(i,s);
}
}
while(!empty(s)){
temp=top(s);
pop(s);
if(empty(s)) area=a[temp]*i;
else area=a[temp]*(i-top(s)-1);
if(max<area) max=area;
}
return max;
}
int main()
{
int n, m;
scanf("%d %d", &n, &m);
int a[n][m];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d", &a[i][j]);
int max=maxarea(m, a[0]);
int area;
for(int i=1;i<n;i++){
for(int j=0;j<m;j++){
if(a[i][j]!=0) a[i][j]+=a[i-1][j];
}
area=maxarea(m,a[i]);
if(area>max) max=area;
}
printf("%d", max);
return 0;
}