forked from SR-Sunny-Raj/Hacktoberfest2021-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeforces_745_C_solution
141 lines (126 loc) · 3.66 KB
/
codeforces_745_C_solution
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//manish kumar patel
/* PLEASE MERGE THIS IN THE LABEL OF hacktoberfest-accepted as a part of my genuine work for hacktoberfest*/
//thanks
//problem link
// https://codeforces.com/contest/1581/problem/C
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>=b;i--)
#define err() cout<<"--------------------------"<<endl;
#define errA(A) for(auto i:A) cout<<i<<" ";cout<<endl;
#define err1(a) cout<<#a<<" "<<a<<endl
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<endl
#define pb push_back
#define all(A) A.begin(),A.end()
#define allr(A) A.rbegin(),A.rend()
#define ft first
#define sd second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define V vector<ll>
#define S set<ll>
#define VV vector<V>
#define Vpll vector<pll>
#define endl "\n"
ll binaryexp(ll a,ll b,ll i)
{
if(b==0)
return 1LL;
if(b==1)
return a;
ll k=binaryexp(a,b/2,i);
if(b&1)
{
return (((k*k)%i)*a)%i;
}
else
return (k*k)%i;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
clock_t tStart = clock();
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t;
cin>>t;
// t=1;
while(t--)
{
ll n,m;
cin>>n>>m;
VV a(n,V(m));
rep(i,0,n)
{
string s;
cin>>s;
rep(j,0,m)
{
if(s[j]=='1')
{
a[i][j]=1;
}
else
{
a[i][j]=0;
}
}
}
VV dp(n+1,V(m+1,0));
rep(i,0,n)
{
rep(j,0,m)
{
dp[i+1][j+1]=dp[i][j+1]+dp[i+1][j]-dp[i][j]+a[i][j];
}
}
VV up(n+1,V(m+1,0));
VV left(n+1,V(m+1,0));
rep(i,0,n)
{
rep(j,0,m)
{
left[i+1][j+1]=left[i+1][j]+a[i][j];
}
}
rep(i,0,n)
{
rep(j,0,m)
{
up[i+1][j+1]=up[i][j+1]+a[i][j];
}
}
ll ans=INT_MAX;
// ll ind1,ind2;
rep(i,0,n)
{
for(ll j=i+4;j<n;j++)
{
ll mm=INT_MAX,idx=0;
for(ll k=3;k<m;k++)
{
ll outer=dp[j+1][k+1]-dp[i][k+1];
ll inner=dp[j][k]-dp[i+1][k];
if(mm>dp[j+1][k-3]-dp[i][k-3]+2*dp[i+1][k-3+1]-2*dp[j][k-3+1]-2*(k)+4+a[i][k-3]+a[j][k-3])
{
mm=min(mm,dp[j+1][k-3]-dp[i][k-3]+2*dp[i+1][k-3+1]-2*dp[j][k-3+1]-2*(k)+4+a[i][k-3]+a[j][k-3]);
idx=k-3;
}
ans=min(ans,2*inner-outer+2*(j-i+k)+mm-2+a[i][k]+a[j][k]);
}
}
}
cout<<ans<<"\n";
}
#ifndef ONLINE_JUDGE
printf("\nRun Time -> %.10fs\n", (double)(clock()-tStart) / CLOCKS_PER_SEC);
#endif
}