-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdreptunghiuri5.cpp
57 lines (54 loc) · 1.64 KB
/
dreptunghiuri5.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
56
57
# include <fstream>
# include <algorithm>
# include <cstring>
# define NR 1005
using namespace std;
ifstream f("dreptunghiuri5.in");
ofstream g("dreptunghiuri5.out");
int i,j,n,m,lastok,VV,VVnou,k,ok;
int a[NR][NR], sus[NR][NR], st[NR];
long long sol;
int main ()
{
f>>n>>m;
for (i=1; i<=n; ++i)
for (j=1; j<=m; ++j) {
f>>a[i][j]; a[i][j]=1-a[i][j];
if (a[i][j]) sus[i][j]=1 + sus[i-1][j];
}
//in stiva mentin mereu numere strict cescatoare !!!!
for (i=n; i>=1; --i) {
for (j=1; j<=m+1; ++j) {
if (a[i][j]==0) { //trebuie sa eliberez stiva
for (k=VV; k>=1; --k) {
if (lastok >= st[k]) ++sol;
st[k]=0;
}
VV=0; lastok=0;
}
else {
if (sus[i][j] > sus[i][st[VV]]) st[++VV]=j;
else if (sus[i][j] == sus[i][st[VV]]) i+=0;
else { //trebuei sa scot
VVnou=VV;
for (k=VV; k>=1; --k) {
if (sus[i][st[k]] > sus[i][j]) { // il scot
if (lastok >= st[k]) ++sol;
sus[i][st[k]]=sus[i][j];
VVnou=k;
}
else if (sus[i][st[k]] == sus[i][j]) {
VVnou=k; break;}
else { // il pun
break;
}
}
VV=VVnou;
}
if (a[i+1][j]==0) lastok=j;
}
}
}
g<<sol<<"\n";
return 0;
}